Chapter 39, while also dealing with a topic of a sexual nature, is a much more common one to be read in children's Sunday school classes than the previous one, probably mostly due to the fact that Joseph is giving a much better example of model behavior than anyone in the previous chapter. (The most virtuous person in the previous chapter was Tamar, and I hardly think a Sunday-school teacher is going to tell kids, "So remember, girls, if your husband won't get you pregnant, dress up like a prostitute and fool him into doing it." Although of course, some husbands go for that sort of thing, no doubt. 'Nuff said...)
So Joseph is sold to Potiphar, a prominent politician in Egypt. (Who sold Joseph? As I said back in chapter 37, I believe "Ishmaelites" in this case is a case of technically inaccurate labeling.) It's a bit interesting to note that Potiphar is never labeled by name in this story other than in this first verse, perhaps the author's intent to highlight the relationship as Joseph's "master" to highlight further Joseph's position as a slave. Joseph quickly rises to a position of prominence in his master's house, because everything put into his hand prospers. (There's a very interesting facet of the whole story of Joseph and the prominent use of the word "hand". Joseph falls into the hand of his brothers, who give him over to the hand of the slave-traders, who sell him to an Egyptian who puts his household in Joseph's hand. Because he leaves his garment in the hand of his master's wife, he ends up in prison, where all matters are put in his hand there, as well. Of course, ultimately, all of this is in the hand of God.)
Eventually, perhaps in part because Joseph is a pretty good-looking guy (a possible translation of verse 6), his master's wife starts coming on to him. Joseph keeps saying no, giving as his primary reason that it would be "sin against God", but one day they end up alone. She grabs on to his clothes and insists that he come to bed with her, and he takes off, leaving his clothes behind. She apparently decides that she's been humiliated, and launches a smear campaign against Joseph as revenge. She tells all the servants, and her husband.
The odd thing about this story is that Joseph ends up in prison. Maybe there's something more to ancient Egyptian culture that I am unaware of, but you might imagine that in many cultures that had slavery, if a slave were to attempt to rape their master's wife, it simply would be the end of them. Potiphar gets angry, but he doesn't kill Joseph; he puts him in prison. I've heard it said that most likely, Potiphar knew that his wife was lying, or at least exaggerating, and what he was really angry about was that he lost a great servant in Joseph because he had to do something to save face in response to his wife's story.
Joseph may wonder why he keeps doing all the right and moral things, but he keeps having all this misfortune. But it turns out that God is still keeping watch over him, and the last few verses of this chapter are very similar to the first few; Joseph has a new master of a sort in the jailer, who once again puts him in a high position over his affairs. And of course, there's still more good to come.