Judah had gotten married and had three sons: Er, Onan, and Shelah. When Er came of age, Judah found him a wife, a woman named Tamar. But Er was evil (we are not told why), and God took him. After Er died, the tradition of the times was that his brother must marry the widow, and the children of this marriage would be considered the children of the dead elder brother and would continue his line. So Judah had Onan marry Tamar. But Onan was not interested in supporting a family that was not considered his own, so when he slept with Tamar he would not ejaculate inside her. This greatly displeased the Lord who had commanded his people to ‘be fruitful and multiply’, and Onan, too, was taken. Next, Shelah was supposed to marry Tamar, but Shelah was still a boy. So Judah told Tamar to return temporarily to her own father as a widow, and when Shelah was grown up they would be married.
Shelah grew to manhood, but Judah did not contact Tamar. She seemed a bit of a jinx, and he did not wish to lose his only remaining son.
A long time afterward, Judah’s wife passed away. After the period of mourning, he took a trip to the city of Timnah, and Tamar heard that he was going there. She also knew that Judah had not done as he should have, and as he had promised, for Shelah was by now a fully grown man. Since it was her divine duty to bear children, and she was considered to be betrothed to the family of Judah and could not marry anyone else, she devised a shrewd plan:
She took off her widow’s garb, covered her face, and sat beside the road that led to Timnah. When Judah saw her, he did not recognize her and thought that she was a harlot, and he asked to sleep with her. She asked what he would pay. He offered a young sheep from his flock. She agreed, but since he did not have the sheep with him, and was only promising to send it later, she asked for some collateral. Judah gave her his seal, cord and staff. They then slept together and she conceived. He later sent a friend to give her the sheep and retrieve his belongings, but she was nowhere to be found and no one in the region had seen any harlots.
Some months later, Judah received word that Tamar was pregnant. Since she was betrothed to his family, and Shelah had not married her, Tamar was evidently guilty of adultery, a capital offense. Judah therefore ordered that she be brought out and burned. As she was being brought out, she sent a package and a message to her father-in-law: “I am with child by the man to whom these belong. Examine these: whose seal and cord and staff are these?” Judah recognized them, and he said “She is more righteous than I, inasmuch as I did not give her to my son Shelah.” This public confession of wrongdoing is the first such confession in the Bible (long overdue since Adam first blamed Eve and Eve blamed the Serpent), and it is a symbol of profound repentance. Judah has learned to be honest, to bear responsibility, and to be merciful.
Tamar was spared and gave birth to twins, Perez and Zerah. King David was a descendant of Perez, and so would be the Messiah.