
"Yosef was brought down to Egypt. He was purchased by Potipha, an officer of Pharaoh, the chamberlain of the butchers, a prominent Egyptian, purchased from the Ishmaelites who had brought him down there."(39:1) Rashi says there is a direct connection (Semichut) of the story of Tamar and the story of the wife of Potiphar for two reasons.
The first is that Yehuda was demoted by his brothers from his high position because of the sale of Yosef, and secondly to indicate that just as Tamar acted with pure motives (Leshem Shamayim)(for the sake of heaven) so too did Potiphar's wife.
She saw in her astrological signs that she was destined to have children descended from him but she did not know whether it would be from her or from her daughter. Clearly she saw greatness in Yosef. Rabbi Bezalel Rudinsky asks, what was Leshem Shamayim? The wife of Potiphar saw she was going to have children from Yosef? How is this for the sake of heaven? How can Rashi compare Tamar's selfless deed with that of the wife of Potiphar who was evil? Did she not cause Yosef to languish in jail for 12 years by telling falsehoods? It is brought down that the daughter of the wife of Potiphar, who was called Osnat Bat Potiphera (41:45), was none other than the daughter of Dena and Shechem.
This brings up the whole question of how the Kings of the Jewish People were offspring of unusual relationships. I will only touch on this matter which calls for a very lengthy discussion. For now I will concentrate on why Malchut Beit Dovid (The Davidic Kingship) is based upon Yibum (Levirate Marriage). It started with Tamar. The Ramban says that Yibum existed even before Matan Torah (The Sinaitic Covenant). Although Tamar was supposed to marry Shelah (the third son of Yehudah) she selflessly gave herself to Yehuda himself in a form of Yibum. This started the Davidic Dynasty. Ruth also was given to Boaz in a Levirate marriage.
This completed the Davidic Dynasty. In order for the Jewish People to have a King, the King has to be totally, 100% devoted to his people. In other words he must be totally selfless. He is the heart and has to give his own heart to the entire people. He cannot be involved with himself. The Rambam, in Hilchot Melachim Chapter 3, Halacha 1 says that the King represents the people's heart. He must be attuned at all times to the needs of the nation. If he turns inward, feeding his own ego it will cause a tremendous danger. That is why the King of Israel has limited number of wives, limited gold and silver, and limited number of horses. Shaul's downfall was due to the fact that he had turned inward, and sought the flocks of the Amalekites. He deviated from the directions given by the prophet Samuel. He was thinking of himself. As a side point Rabbi Rudinsky points out that the High Priest wore Shatnes (Linen woven with wool). Only the High Priest who was the Spiritual Leader was allowed.
The High Priest also had to be totally devoted to the Jewish People. If he let his ego get in the way he would fail. The only reason he serves is because of the people. If he wore Shatnes for his own pleasure and not for the work on behalf of the People then he would not be permitted to do so.
He is only allowed to wear Shatnes if he is working 100% for the People. G-d only permits him to wear Shatnes when he is devoted entirely to the people. The political leader and the spiritual leader must sacrifice his whole life and his whole heart to the Jewish People. Yibum is selfless too. Yibum is an act whereby one is willing to make his own flesh and blood somebody else's.You are sacrificing yourself for the person who perished. Yehuda was selfless. He nullified himself. Tamar similarly nullified herself. Therefore the wife of Potiphar and Tamar were capable of bearing Kings.
Tamar for sure nullified herself for the sake of heaven. The wife of Potiphar too at least had an idea that it was for the sake of heaven. She was privileged to have her daughter marry Yosef. This is what Rashi is intimating when he compares the wife of Potiphar and Tamar. ("that just as Tamar acted for the sake of heaven so too did Potiphar's wife act for the sake of heaven.") Even though the wife of Potiphar was eyeing Yosef for her own pleasure, she had the notion that Kings would be descended from him. She saw his potential greatness and the greatness of his descendants. This, according to Rashi, was a plus. Her daughter became Yosef's wife instead.
One statement on why the Davidic Dynasty was derived from what appeared to be illicit relationships. G-d always hides his treasures in the "mud". I have stated this in regards to G-d's Holy Sites like Yerushalayim (owned originally by the Jebusites) and Chevron (owned by the Hittites). G-d uses the Sitra Achra ("Dark Side") to hide Holiness.
Much of this is difficult to comprehend. The making of the Davidic Dynasty is full of questions. The mind of Man cannot fathom all the intricacies. It is proof positive that G-d, Almighty runs the world. Shabbat Shalom amd Chag Chanukah Sameach.