In this chapter, Judah goes away from his family, marries a Canaanite, has several sons die, and then has sex with his daughter-in-law.
I think my title sentence says it all: this is a very licentious chapter. This chapter describes quite a few events, some of which have important implications, and most of them are very risque.
First, Judah "leaves his brothers" and goes to an unspecified location elsewhere. We can guess he is still in Canaan (the promised land) because he marries the daughter of a Canaanite it later says he "goes up to Timnah" which is in biblical Israel. It's peculiar that he would leave his brothers given they are sharing the common task of shepherding their father's flocks, and one wonders if this is related to their actions against Joseph in the prior chapter. However, ultimately Judah's motives are not described. One interesting point I've heard raised before is that Judah in this instance is acting as a "prodigal son", in that he leaves his family, engages in what can easily be construed as sinful behavior, and later (4 chapters from now, when we see Jacob with his sons again), Judah is returned to his family with no overt discussion of what he had done or how he was repatriated into his family. Later still, Judah is given the second best blessing from his father Jacob, so that while the sins of Reuben, Simeon and Levi are held against them, Judah is in effect treated as the oldest accepted son, in spite of his own sins. This is probably because the first three, Reuben, Simeon and Levi sinned against their fathers in various ways, but Judah never did anything to spite him. Possibly his father never even learned of what Judah did while he was away. We don't know.
Second, Judah bears three sons in this time, which means that he was gone for a *minimum* of 27 months. In addition, he takes a wife for his oldest son, which means that he is likely over 25 years old, maybe up to 30 or 35 since men in this time tended to marry at a late age while they built up their wealth. So it is likely that this story about Judah is happening during the years that Joseph is in captivity in Egypt, and it also shows that Judah's departure from his brothers was a serious, multi-year thing, reminiscent of Jacob's 20 years in Haran.
Then there is the story of Er's death and Onan's betrayal to his brother's legacy. This is a subject that deserves some explanation. For some of the historical aspects of how this has influenced church-approved theology, this appeared to be a good resource:
I cannot vouch for their accuracy since I am largely unfamiliar with the subject, nor do I vouch for the website in general, but this particular article seemed to be well-formulated and accurate as far as I can tell.
The main thing to draw from this is that the "sin of Onan" has been broadly interpreted in church history to refer to either masturbation or birth control methods in general, anything that would inhibit the formation of a valid embryo during sex (this would include post-sex birth control like Plan B, and would certainly also include homosexuality).
My opinion is what Straight Dope calls the "straightforward interpretations", that the sin of Onan is his refusal to provide an heir for Er like he is supposed to. I think they are absolutely correct that this is the simplest interpretation, and I believe it is the right interpretation. While the "alternative" interpretations can in part be supported by unrelated arguments (and that is a complicated subject I will not approach here), I simply do not believe they are supported by this text. Anyone new to the bible would see this right away: Onan was commanded by his father (and by their social system of the time) to produce offspring for his brother through the pact known as levirate law (this term is unrelated to Levi, the son of Jacob). He ostensibly agreed, but then during sex would attempt to avoid insemination of Tamar, thereby abrogating his duty.
Some important things to consider:
The levirate law discussed here is *not yet* part of the bible, and Onan is under no divine command to obey the levirate law and impregnate his sister-in-law. This is purely a social contract at the time, but it is relevant to note that the Law of Moses will institute a levirate law for the Israelites, in the book of Deuteronomy. However, even though Onan is not under the Law of Moses, he is responsible to maintain his personal integrity, which includes a lot of things in this time period like obedience to his father and truthfulness in action and deed. Having sex with Tamar but refusing to inseminate her is not that.
One might ask what is the importance of producing a son for the widow in this manner? I agree that the whole process seems very strange, if not perverse, to a modern reader, so it is very important to get the cultural context set. This is something I have done frequently in prior chapters, so I will be relatively brief. 1) The woman's importance is related to bearing sons for her husband, so as to carry on his name, 2) bearing sons allows the man's inheritance to be given to them, extending the family line and the man's name (this is why people are called Jacob SON OF Isaac, because it extends Isaac's name and grants him a legacy or memorial that is meant to last forever). 3) the son would grow up and support his mother in her old age. This is critically important because their society did not possess a public social safety net of any kind. This is also why Tamar goes on to seek a child through Shelah and then eventually Judah, because without a son, she is left with the very serious possibility that Judah would depart and leave her destitute and without a source of income, and this would be a severe violation of the social contract of the time.
Lastly, what is the important to Onan of *not* producing a son for his brother? This is touched upon briefly by Straight Dope. The bible does not say what was his motivation, so we are largely left to speculation. But of those speculations, the best answer I am aware of (which is also mentioned by Straight Dope) is that Onan wanted to take his brother's inheritance, since without an heir those properties would fall to him as the closest kin. Providing a son would run contrary to this goal. This is the most reasonable speculation I've heard. It is closely related to the kinsman-redeemer issue that shows up in the book of Ruth (Ruth chapter 4).
After a while, it became obvious to Tamar that Judah was not going to provide Shelah to her, so she takes matters into her own hands and literally prostitutes herself out to Judah while disguised. The sins of Judah are manifold in this chapter. He refuses his son Shelah to Tamar, even though that was her legal right, and he lies to her about it continuing the pattern of deception that we saw from Jacob. Then Judah goes and has sex with her when he thinks she's a temple prostitute (which would be a grievous sin to the Israelites reading this book), condemning her to death when she is found pregnant, which is a hypocritical contradiction of his own willingness to sleep with a temple prostitute. He slept with his daughter-in-law, which is also forbidden by the Law of Moses. It is astonishing to think that, having struck down Er and Onan, God did not strike down Judah as well. I don't really have an explanation for that, other than the sparsity of the biblical text concealing much of what is happening.
One can only imagine Judah's relief at discovering Tamar has committed prostitution. This finally allows him, in a fully righteous manner, to kill off this awkward woman whom he was doing wrong. It would keep him from ever having to provide her a husband, and would also prevent her from telling others how he had wronged her, and he would never have to do anything illicit or illegal to have her killed. She walked into this situation as if by her own accord. In verse 26, Judah discovers what he had done, and it is strongly implied that he calls off her execution (which he had declared in verse 24), and by this she bears two sons for Judah, one of whom, Perez, becomes the ancestor of King David.
It is also important to remember that through all of this, Judah's friendship with the Adullamite (mentioned several times), his Canaanite wife, and the Canaanite wife for his sons (Tamar would almost certainly be Canaanite), Judah is repeatedly breaking the principle of separation from the sinful peoples in the promised land. As I've said before, this is not a legally binding requirement for them, but the Israelite(s) writing the text, and definitely the ones reading it, would have a strong cultural antagonism towards commingling with the idolatrous peoples. There is little doubt in my mind that, while it would not have been directly sinful for Judah to do this, it is at least implicitly related to his various moral failings in this timeframe.
My conclusion? Out of this incredibly awkward and repeatedly sinful passages from multiple figures (Er, Onan, Judah), an ancestor of King David, and through him Jesus Christ, is born. This is like a microcosm of the larger bible, where the vast majority of what goes on is deeply sinful behavior from most of the actors, and yet through this giant mess, God works in a pattern of grace, that the illicit child of Judah and a prostitute would go on to father King David and bring about the great restoration of the Israelite Kingdom from the hands of Saul, the most glorious period in Israel's history. This is just one link in the chain, Perez having many ancestors before him and many descendants after, but in his birth he is perfectly representative of much of what happens before and since.
On a minor note, please note that Perez, the younger of the twins, is the ancestor of King David and the Christ. This continues Genesis's pattern of establishing the supremacy of the younger child (Isaac over Ishmael, Jacob over Esau, now Perez over Zerah). To be fair, both Perez and Zerah have an older brother in Shelah, so it's not an exact parallel, but it's close enough to be worth mentioning.