I've always been a bit puzzled by the account of Judah and Tamar in Genesis 38. From the point of view of crafting a flowing narrative it seems a discordant digression. It occurs immediately following the selling of Joseph into the hands of the Midianites who take him down to Egypt and sell him to Potiphar. Why does Moses insert the rather unflattering story of incest here? What does he intend the Israelites to learn from this story, especially given the later positive details concerning Judah? I've struck upon some plausible reasons in going through Genesis with my seventh grade theology students this year, which I'll share here.
Several details within the story, as well as some of the ways in which earlier narratives seem to "work" for Abraham, Isaac, and Jacob help to make sense of the story of Judah and Tamar.
First, in the earlier accounts of Abraham, Isaac, and Judah we discover that God often brings each patriarch through various difficulties and threats (some of which are of their own decisions) in order to grow them up into the kind of individuals whose thoughts and actions are consistent with God's own. For example, Abraham would have been unlikely to have offered up Isaac, believing that God would raise him from the dead, if God had not already delivered Abraham through the threats of a Pharaoh in Egypt, delivering Lot from Chedorlaomer, delivering Lot from the destruction of Sodom and Gomorrah and so on. Similarly, Jacob the schemer was himself schemed, yet God allowed him to flourish despite Laban's mistreatment. By this humiliation, Jacob learned how to trust in God and instead of scheming Laban and Esau, acted righteously with both, even though he was fearful in the latter case.
So, knowing that Judah is going to be the bearer of the promised line of Messiah, the heir of the promise of God to Abraham, Isaac, and Jacob; God must discipline Judah in order to prepare him for the responsibilities of carrying on the promise. The story of Judah and Tamar plays an important role in showing the development of Judah's character, and the importance of why he carries on the promise rather the Reuben, Simeon, or Levi.
As for the details that Moses may be using to indicate the relevance of the Judah and Tamar story, several stand out for notice. First, Genesis is riddled with the themes of famine and barrenness. Both are consequences of the Fall and bring death to man--the barrenness of the ground and the barrenness of the womb are both reversals of the commandments to be fruitful, multiply, and fill the earth while care-taking for the earth through dominion. However, there is also a theme of life resulting from death, as when famine (death) drives Abraham into Egypt, yet God brings him out of Egypt with great wealth. We thus see that death characterizes life on earth, but for the promised people of God, death is swallowed up in abundant life.
In Genesis 38, however, we see that Judah is literally cutting off life from his own family by not allowing Tamar to marry Shelah. Judah's fear for Shelah's life shows a lack of understanding of what protects life (God's promise and walking righteously before him), and in a vain effort to protect the life of Shelah, he is actually threatening the Messianic promise, which comes through Tamar.
Similarly, Jacob withholds his youngest son, Benjamin, which is cutting off life from his own family, for it is only through Benjamin's going down to Egypt with his brothers that Joseph will receive them and allow them to buy grain to alleviate the famine. Jacob clings to the life of Benjamin, but in doing so threatens the lives of his family and therefore the Messianic promise.
In order to receive what is rightly hers, Tamar engages in a deceit by which Judah's unfaithfulness will be uncovered and the opportunity for restoration to be made. Similarly, Joseph's deceit of his brothers is a chance to uncover their unfaithfulness and give them an opportunity for restoration to be made. Although Tamar looks like a harlot, it is Judah's unbelief that is adulterous in the eyes of God. Although Joseph looks like a wrathful master, it is the envious brothers who had dealt with Joseph in wrath.
When she deceives Judah, Tamar takes his signet and cord and his staff--the signs of his authority. She takes from him what he has abdicated. When Joseph deceives his brothers, he plants upon Benjamin his "divination cup," which is the sign of his authority and recalls the prophetic dreams that the brothers should have acknowledged. Joseph gives up what had been usurped already by the brothers--his prophetic right to be in authority over them.
When Judah's sins are exposed by Tamar's deceit, he repents of his unrighteousness and receives her back into his family where he had before excluded her by his refusal to offer Shelah--he honors his responsibility to his children as a father of his clan. When Judah's sins are exposed by Joseph's deceit, he repents of his unrighteousness by offering up his own life for Benjamin's that he might honor his father Jacob--the father of his clan.
Judah knows what it is to jealously love a son, and he recognizes what it cost Jacob to give Benjamin away. Judah stands as a father to Benjamin in the absence of Jacob, and so redeems Benjamin's life by offering his own. He has learned from his failure to give up his own son to Tamar and now thinks and acts as God the Father thinks and acts--giving of Himself in order to redeem another.
There are other details in the Judah and Tamar story that I've not come up with explanations for at this point (like the significance of the scarlet thread, the names of Shelah and Perez), but I think I've found a plausible exposition of the text that harmonizes with the larger narrative of Joseph, the larger narrative of Genesis, and the trajectory of Redemptive history. It is all very rough, as I've only recently been thinking on these details, and I'd appreciate any further observations or criticisms that my readers have to offer.