"It came to pass at that time that Judah departed from his brothers, and visited a certain Adullamite whose name was Hirah. And Judah saw there a daughter of a certain Canaanite whose name was Shua, and he married her and went in to her. So she conceived and bore a son, and he called his name Er. She conceived again and bore a son, and she called his name Onan. And she conceived yet again and bore a son, and called his name Shelah" (Gen. 38:1-5).
Abraham had a strong conviction regarding the importance of acquiring a non-Canaanite wife for his son, but it seems this notion was deemed less and less important by each passing generation. Esau married Canaanite women, and Genesis 38 records Judah's marriage to a Canaanite woman and the disastrous results that followed as his sons grew up and became men.
Judah selected Tamar as wife for his firstborn son, Er. Although the details are not revealed, Moses affirmed that Er "was wicked in the sight of the LORD, and the LORD killed him." In what way was he wicked? How did God take his life? There is one thing we can know for certain: The wicked influence of the Canaanites was negatively impacting Judah's family in a significant way. Onan, the second son, was encouraged to take Tamar as his wife, in order to raise up an heir to his deceased brother (which was a common custom of that day). But, he, either due to selfishness or greed, refuses to do so. He purposefully ejaculated on the ground instead of inside Tamar. His behavior displeased the Lord so much that "He killed him also" (38:10). Judah then instructed Tamar to go back to her father's house and live there until his third son was of marriageable age. In truth, Judah should have taken care of Tamar and not sent her back home to her father. But, as the narrative unfolds, it appears that Judah has no intention of giving his third son to this woman.
"And it was told Tamar, saying, 'Look, your father-in-law is going up to Timnah to shear his sheep.' So she took off her widow's garments, covered herself with a veil and wrapped herself, and sat in an open place which was on the way to Timnah; for she saw that Shelah was grown, and she was not given to him as a wife. When Judah saw her, he thought she was a harlot, because she had covered her face. Then he turned to her by the way, and said, 'Please let me come in to you'; for he did not know that she was his daughter-in-law. So she said, 'What will you give me, that you may come in to me?'" (Gen. 38:13-16).
Judah's wife had died shortly before this and he is staying busy visiting friends and tending to his sheep. But, his yearning for sexual intercourse is strong, and he sees a woman whom he believes to be a prostitute and makes a contract with her. He would give her a young goat for sex, and he would provide a pledge in the meantime (i.e., unique items that would identify him). Why did Tamar pursue this course of action? We might speculate that her primary desire was for children. What Judah had withheld from her (by not giving her to Shelah in marriage), she would acquire in her own perverse way--offspring!
"And it came to pass, about three months after that, that Judah was told, saying, 'Tamar your daughter-in-law has played the harlot; furthermore she is with child by harlotry.' So Judah said, 'Bring her out and let her be burned!' When she was brought out, she sent to her father-in-law, saying, 'By the man to whom these belong, I am with child.' And she said, 'Please determine whose these are--the signet and cord, and staff.' So Judah acknowledged them and said, 'She has been more righteous than I, because I did not give her to Shelah my son.' And he never knew her again" (Gen. 38:24-26). Judah is quick to command the severest of punishment, yet Tamar was shrewd enough in keeping the personal items of Judah to guarantee her own safety. Judah confesses his sin (i.e., in preventing Shelah from marrying her as he had promised), and God is gracious to both Judah and Tamar.
The chapter closes with the birth of twin sons--Perez and Zerah--to Tamar and Judah. This is the second recorded set of twins in the Bible (cf. 25:24). Even though there was so much wickedness in the family of Judah, God would choose to have the Messiah born into that family line centuries later (cf. Matt. 1:3).
From narratives like this one, it is easy to see why God would send the Israelites into Egypt where they would be looked upon unfavorably as foreigners, especially as shepherds. This will end up forcing them to remain distinct as a people, which was necessary in order to properly fulfill all the promises to Abram. This may be the primary reason why the content of this chapter was recorded under inspiration of the Holy Spirit.