The story of Judah and Tamar in chapter thirty-eight is filled with practices and customs that will be unfamiliar to most readers.
There is the matter of the “levirate marriage” (Deuteronmy 25:5ff), which is when a brother dies leaving no heir. The idea is that the surviving brother will take the widow and have children with her. Her children are counted as the children of her dead husband and his heirs.
If you think that explanation is confusing, just try living with it.
Evidently no one liked it, judged from the stories about it in the Old Testament (though it does seem to work later for Boaz and Ruth).
Who will be Judah’s heir?
Legitimately, the inheritance of the firstborn belongs to Er and his descendants, and Tamar has every right to expect that will happen. But considering both of the sons he has given her to have died, she seems to be regarded as a piece of bad luck by Judah and he refuses to give her to his remaining son. It is a horrid statement about Judah’s ethics, but the statement doesn’t end there. He doesn’t mind having sex with a prostitute, but of his daughter-in-law has sex, she will be burned.
We are not told what Er’s sin was, but it was bad enough for God to kill him. Onan’s sin is much easier to understand. He didn’t mind having sex with Tamar, but he surely wasn’t going to produce children by her – the only legitimate reason for him to be intimate with her. (English Bibles sometimes obscure the fact that Onan had sex with her repeatedly. It was not a one time affair.) To see how God feels about using others purely for sexual pleasure, note that God killed Onan too.
Why do we have this story?
It informs us why Judah’s firstborn heirs are the children of Tamar, which will become important later regarding genealogy. But it also provides us with insight to the sinfulness of Jacob’s family. It will make the story of Joseph that much brighter.