The story is in Genesis 38 and is worth a read if you have not read it before or are not particularly familiar with it.
Briefly, Judah marries and has 3 sons. He finds a wife, Tamar, for his eldest son Er who is put to death by God for his wickedness before they have children. Tamar marries the next son who refuses to conceive children as any children through Tamar will be regarded as Er's brothers offspring not his, and God kills him also. Then Tamar is sent back to her father until the youngest son is old enough to marry her. Judah does not give the youngest to Tamar for fear he would die as well. Judah's wife dies. Tamar disguises herself and Judah has sex with her thinking she is a cult prostitute. On learning of Tamar's pregnancy Judah orders her execution, which is stayed because Tamar proves Judah is the father.
The story is more detailed and more dramatic than my brief summary. I wish to draw out some issues that I think helps understand this story. I used to think that Tamar's behaviour was wrong, just less wrong than Judah's; now I am more inclined to defend Tamar's actions.
Dealing with Judah's sons Er and Onan firstly. We are not told of the sin of Er, not that it matters greatly. God put him to death for his wickedness. As God is the ultimate judge; all human judges gaining their authority from him. If Er's actions warranted death in God's sight then he deserved death. And if God enacted that judgment then so be it.
The idea that a man had children for his deceased childless brother thru the widow appears to be an ancient custom. Moses describes the laws surrounding this situation in Deuteronomy 25 though this is over 200 years later. In cultures for much of history, and especially thousands of years ago a woman relied on her father for protection and provision. On marriage this responsibility passed to the husband. Now one's identity was strongly related to his family: posterity for men and raising children for women. A widow is left without protection and support. Childless women carried the shame of their condition, raising children being the focus of her role in society. Children also provided for one in his old age. So a childless woman has the shame of childlessness (though maybe not to the same extent as infertility), no child to help support her as she ages, and no husband to protect or provide for her at the time. This left her particularly vulnerable.
The brother in such society was to marry the widow. This immediately gave the widow provision that she lacked, but also the promise of children. The first child (or children) would be considered as a descendant of the deceased husband so as to preserve his name in future generations. The brother was able to take his own wife and have his own family. Of course some of his income would go to support his brother's widow. Note that Judah describes the marrying of a widow as a duty.
Then Judah said to Onan, "Go in to your brother’s wife and perform the duty of a brother-in-law to her, and raise up offspring for your brother."While Onan took Tamar as his wife, his refusal to enable her to conceive was both an affront to Tamar as she was unable to be a mother, and an affront to his dead brother as he would be left without posterity. It was a very harsh situation. Essentially there was no other option for Tamar, as long as Onan was alive Tamar was unable to remarry yet she would stay childless and Er may remain without remembrance. I tend to think God was probably more concerned with Onan's mistreatment of Tamar than Er's sin. Onan's sin entailed significant mistreatment of others and for such wickedness God put him to death. We see elsewhere in Scripture God's great concern for the defenceless such as widows and orphans.
After Onan's death Tamar was now free to remarry and the duty now fell to Judah's next son Shelah. Apparently Shelah was too young and Judah sent Tamar back to her father. The real reason is that Judah feared for Shelah's life. A question is raised as to how old was Shelah and was he really too young. More importantly was it right for Judah to send Tamar back to her father. I am uncertain as to whether Judah should have taken responsibility for Tamar and provided for her in his own house. This seems possible based on the comment that Judah feared for Shelah, that is, sending her away alleviated Judah's fears.
Judah failed to give Shelah to Tamar when Shelah was of age. This is mistreatment of Tamar by Judah; she is in the same situation as when she was with Onan: unable to remarry (as pledged to Shelah) and unable to have children. Tamar's solution is to goes up to meet Judah in disguise. It is uncertain whether Tamar intended to appear as a prostitute in order to have sex with Judah, or whether she responded to his request for sex.
When Judah saw her, he thought she was a prostitute, for she had covered her face. He turned to her at the roadside and said, “Come, let me come in to you,” for he did not know that she was his daughter-in-law. She said, “What will you give me, that you may come in to me?”The text may read that he thought she was a prostitute because of the veil. However it possibly just implies that he thought she was a prostitute because she was sitting at the entrance to Enaim and he did not recognise her because her face was covered with a veil. Judah's friend Hirah calls her a cult prostitute (qedeshah).
Tamar takes a pledge then leaves before Judah can redeem it. Tamar dresses like a widow again. Judah finds out she is pregnant and demands she be burned. Tamar was betrothed to Shelah so getting pregnant is proof of adultery. This was punishable by death. Adultery is a more serious crime than sex outside of marriage or prostitution by a woman who is not betrothed or married. The request for death by burning seems a little excessive by Judah. It is prescribed in Leviticus 21 for daughters of priests who become prostitutes, possibly because priests are to be holy.
Tamar returns the pledge to Judah making him aware the child is his. Then we hear this interesting comment by Judah
She is more righteous than I, since I did not give her to my son Shelah.This is not necessarily claiming Tamar is righteous, the comparison is with him. Judah is acknowledging that his behaviour is worse than Tamar's. Judah is not claiming that he is indeed righteous, thus he need not be claiming that Tamar is either.
Nevertheless, I think Tamar's choice may be acceptable, and if not the best decision, surely an understandable response of a desperate woman. A key to understanding this episode is that Tamar's behaviour with Judah had nothing to do with sex. It was that from Judah's point of view, but for Tamar it was merely the means for her to gain the status and responsibility that she had failed to obtain; first because of her wicked husband, then because of her wicked brother-in-law, then because of her fearful and unfaithful father-in-law. It was right that Tamar be treated righteously, yet she wasn't. Perhaps she should have let God fight for her as we learn elsewhere in Scripture, but interestingly we have a Canaanite woman who acts honourably in all her dealings yet is mistreated by the people who worship the true God. The fact that Tamar secured her own rights is of less concern.
Judah makes right his failure to give Shelah to Tamar. He takes Tamar into his own house and provides for her and her children (she had twins), though does not lie with her again. Tamar now has both provision and children, she is honoured by her posterity (Rut 4:12), and she becomes an ancestor of the Messiah (Mat 1:3).