Judah's story never even registered with me on any previous readings of Genesis. As I was taking it in, my jaw dropped. Quite the parallel to Joseph indeed. If y'all don't know what I'm talking about, go read this story now.
Chapter 38 provides a "story of Judah" that is parallel to the story of Joseph in time while being completely opposed in moral tone. It serves to set off the story of Joseph in a number of ways: both leave home, one voluntarily, the other against his will. One leaves to seek his fortune among the Canaanites, the other is sold as a slave to Egypt. One seeks out a prostitute, the other flees sexual temptation. What becomes of these men, who will father the two leading tribes of Israel, is a study in contrast. There is great irony in the outcome, for what appears to be true on the outside (one man moving freely and in control of his destiny; another man enslaved, in control of nothing but his response to the situation) does not take into account the unseen -- the will and the presence of God....All quoted material is from Catholic Exchange's "Catholic Scripture Study." See the sidebar under "Bible Study: Genesis" for links to references used.
Two of Judah's sons were so wicked, God killed them before they had children. According to custom, Judah should have given his third son to the first son's wife so the family name would continue, but he was afraid that son would die too so he sent his daughter-in-law home to her father. This left him with one son who was betrothed to a woman he was not allowed near -- hardly a recipe for building a family. The wickedness of Judah's sons makes one question Judah's ability to "father" properly in any sense of the word -- and yet God had chosen Judah to father the tribe that would one day produce the Messiah, and He would bring that about.
Onan's sin was preventing pregnancy by spilling his seed on the ground. In doing so, he was taking selfish measures to make sure no child would come between himself and his brother's property. But it was not just his intent but the act itself that was wrong. Onan was going through the motions of a covenant act while denying it meaning and purpose. According to the Catechism, "every action which, whether in anticipation of the conjugal act, or in its accomplishment, or in the development of its natural consequences, proposed, whether as an end or as a means, to render procreation impossible -- is intrinsically evil." (#2370)
Tamar, with her courageous plan to get that which was hers by right but which Judah refused her, became the means by which Judah's line -- the line from which the Savior would come -- is continued. This is yet another illustration of the fact that membership in the family of God is determined not by natural order but by God's providence in determining who will be heir to promise and blessing.