Genesis CH 38 - Tamar is the widow of Judah's first son. God saw how wicked Judah's first two son were and put them to death. Then Judah was scared of giving his youngest son to Tamar because his fate might be the same. What was the reason for this story being told in the bible?
The simplest answer is because it happened and the Bible records history whether flattering or not.
The "grand scheme" answer probably has multiple correct and concurrent answers one may be found in the book of Ruth.
Ruth 4:12 and may your house be like the house of Perez, whom Tamar bore to Judah, because of the offspring that the LORD will give you by this young woman."
Deut 23:2 "No one born of a forbidden union may enter the assembly of the LORD. Even to the tenth generation, none of his descendants may enter the assembly of the LORD.
Boaz had nothing to lose and much to gain. The tenth generation from Perez happened to be David. Due to this happening in the past it allowed gentiles to be a part of the bloodline David and later Jesus the Christ. Luke 3:23-32 shows the lineage of Christ back to Boaz, further if you go to vs 38 to Adam.
Ruth 4:18 Now these are the generations of Perez: Perez fathered Hezron,
Ruth 4:19 Hezron fathered Ram, Ram fathered Amminadab,
Ruth 4:20 Amminadab fathered Nahshon, Nahshon fathered Salmon,
Ruth 4:21 Salmon fathered Boaz, Boaz fathered Obed,
Ruth 4:22 Obed fathered Jesse, and Jesse fathered David.
This is a portion of an answer from a previous question. I believe it may apply. What is the significance of seal, cord, and staff?
One can not help notice how the narrative of Judah and Tamar is nestled between the narratives of Joseph's betrayal by his brethren and his entrance in to Egypt. But chapter 38 is of supreme importance because it narrates the continuance of the tribe of Judah, the tribe which the Christ would descend from. Due to the evil deeds by Tamar's first two husbands (Judah's first two sons), the Lord took their lives leaving only one son. Judah's reluctance to give his last son in marriage to Tamar was actually a good thing. Without divine intervention, the line stemming from Judah would have been lost. Keep in mind that Judah's wife Shuah was a Canaanite. And just as Abraham instructed Isaac not to marry women of the Canaanites, (Gen 28:1) the twelve tribes were to heed the same command. Although Judah's intent to fornicate with a temple prostitute was sinful, God's sovereignty preserved the line of Judah from Canaanite infusion.
I've long thought that chapter 38 is Judah's 'metanoia'. In 37, Judah appears to be saving Joseph's life but he is really committing an act of evil by selling his brother into slavery. The pace of the narrative changes in ch38. Judah commits to a Canaanite woman (long forbidden - look back to Abraham) and Judah comes to understand God's condemnation of the evil act of selling his brother into slavery. This is the metanoia, the turning point. After ch38, Judah is committed to the return of Joseph to the bosom of Jacob/Israel and the reunification of the family/nation. God rewards Judah, the one who, though via sin, experiences true conversion.
All good answers above, but I think the primary reason for this interlude in Joseph's story, is to show how likely the very first generation of Israelites could have become Canaanites. Judah left his brothers (due to guilt over selling Joseph, and watching his father die day by day because he thought he was dead?) and married a Canaanite woman, had three sons, and chose Tamar, also Canaanite, as a wife for his oldest son. So much for being set apart as a holy nation! If all of Judah's brothers did the same, the Messianic line could not have been established. Thus the necessity for the Egyptian era, when the Jews were anathema to the Egyptians and confined to Goshen, where they grew in numbers. It then took the Exodus to create them from slaves to a distinct people under God.
Also, it shows that God can, and does, redeem His own and actually use our past (familial hatred, incest) for our good and His glory. No one is outside of the grace of God, unless they refuse Him.