Brotherly love is something that appears to be missing throughout Sefer Breisheet. Starting with Cain and Hevel, to Yitzchak and Yishmael, Yaakov and Eisav and culminating with the story of Yosef and his brothers, it is conflict which is the norm. And the Torah spends many a chapter detailing this most tragic story.
Yet right in the middle of the story - between the sale of Yosef and his arrival in Egypt - the Torah digresses by recording the story of Tamar and Yehuda. Twice widowed from the two older sons of Yehuda, Tamar is told by her father-in-law to "Live as a widow in your father's house until my son Shelah is grown" (Breisheet 38:11). Eager to have a child and not willing to wait, Tamar disguises herself and Yehuda thinking her to be a prostitute has relations with her. When Yehuda finds out his daughter-in-law is pregnant he orders that she be burned at the stake. Tamar refuses to say who had impregnated her, instead revealing only the objects that Yehuda had given her as security for payment. Upon realizing that all Tamar wanted was a child, Yehuda admits his guilt, frees Tamar enabling her to give birth to twins who would be the ancestors of King David.
Not only does this story seem to interrupt the Yosef narrative, it is also out of place chronologically, as many of the events in the story took place before the sale of Joseph. For what reason is it placed here?
Apparently, the Torah wanted to focus on the depths to which Yehuda had sunk. Not only was it his idea to sell Yosef, he had even propositioned someone he thought was a prostitute, and nearly had Tamar mistakenly put to death. Yehuda deceived his father by dipping Yosef's coat in the blood of a goat and asking Yaakov "haker nah", please examine it, is it your son's coat or not?" (Breisheet 37:32). Yehuda was himself fooled measure for measure by Tamar who uses the same expression: "haker nah, identify these objects" (Breisheet 38:25). Yaakov too is subject to the suffering of "measure for measure". He had deceived his own father by putting goat skin on his arms.
The Torah does not hide the critical moral lapses of our great Biblical heroes. Yet Judaism always sees the potential for good even in the worst of circumstances. The Midrash relates that while the brothers were busying themselves with the sale of Yosef, and Yaakov was busy crying, and Yehuda was dealing with his family crisis, G-d was busy preparing the "light of the Masiach". It is the descendants of Yehuda and Tamar who formed the kingdom of David. Provided one learns from one's mistakes, sin can be the precursor to great things.
The story of Yosef and his brothers is one of jealousy, hatred, kidnapping and lying. A tragic story that haunts us to this day, sowing the seeds of hatred amongst the Jewish people. The seemingly endless conflict between the Kingdom of Judah and that of Israel had begun. The story of Tamar - just like that of her descendant Rut - is one of tremendous self-sacrifice in the face of personal tragedy. Commenting on her willingness to die rather than implicate Judah, our Sages teach that, "it is better to throw oneself into a fiery furnace than to embarrass somebody in public" (Bava Metzia 59a). Amazingly, there are rabbinic authorities (see, for example, Responsa Binyan Tzion # 172 here) claim that this is meant literally and joins idolatry, adultery and murder as cardinal sins for which one must be willing to forfeit one's life rather than transgress.
That public embarrassment is equated with murder can be seen in the face of an embarrassed person which turns red, symbolizing the spilling of blood, and subsequently turns white, symbolizing the blood being completely drained from the body.
The story of Tamar is not an interruption of the Yosef story. It is the antidote to the internal strife that afflicts us. The moral courage of Tamar, the admittance by Yehuda that he was wrong; these are the necessary ingredients to bring the Masiach. Yosef was sold but Tamar shows how far one can go, must go, so as not to harm another. The Davidic dynasty is in place, it is our duty to ensure that it is not stillborn.