Genesis 38

2 Judah married a Canaanite woman instead of a woman from his ancestral household. I suspect that Israel would not have liked this, but it is possible that he would be reluctant to go back to Laban's household looking for wives for his twelve sons.
7 It seems apparent that the brothers had not learned to obey and worship God, and thus, would not be taught to their children. When we ignore God, only wickedness can flourish. Er had apparently become so wicked that God destroyed him.
8 Even though the Mosaic codes for this had not been written yet, it was apparently accepted as a duty. The younger son was to have sex with his deceased brother's wife so that she would have a son to take care of her. However, the younger brother knew that this child would receive the inheritance, rather than his own son.
9 However, instead of refusing to do this act, he pretends to do it. Because this deception involved sex, it is very shocking. God destroys him, not only for disobedience, but also for making disobedience look like obedience.
11 Judah didn't want this same thing to happen to his next son, Shelah. Since Shelah was so young, it was easy to delay. The Bible does not say whether Judah knew that the previous two punishments were because of sin. He was either unwilling to encourage his son behaved in a godly manner, or he was superstitious about Tamar.
14 Tamar could see that Judah was not about to keep his duty and promise to have Shelah father a son through her for his brother. But the plot she devises is not a good one. This is obviously a human plan, not God's will. She pretends to be a prostitute, and Judah propositions her because he felt lonely after his wife's death. Later the deception is exposed (as usual) and everyone is embarrassed, and no one is shown to be a godly example for the world.
17 He promised to pay her "tomorrow," but she wanted to take a pledge to guarantee his return. She suggests that he put up a few personalized objects for a "bond."
19 To further the deception, Tamar would go back and pretend that nothing happened for the next several months.
20 I suspect that Judah did not want others to know about his extra-marital affair, so he sent a trusted friend to pay the prostitute and retrieve the personalized items.
21 The friend then has to undergo the embarrassment of asking others where the prostitute went. However, it appeared that this area did not encourage prostitution, so what they were asked sounded ridiculous and made the friend look bad. Judah decides that they should give up the hunt. He lost some of his personal belongings, but he would give them up to save his reputation. He should have looked to please God and protect his reputation earlier.
24 After three months it was apparent that Tamar was pregnant. Now Judah's response was in line with God's righteous punishment (Lev 20:10), but it is obvious to us that his judgement was both hypocritical and incomplete (Lev 20:14, John 8:7).
26 To his credit, when faced with undeniable evidence, he admits his mistake and reverses his death sentence. He did not go as far as marrying her, but she would have a son now, and that was the goal of the situation (although the means were obviously wrong).
27 By having twins, she would be doubly blessed. In the eyes of others, this would be seen as God's vindication of Tamar's actions because of Judah's broken promise.