Hashem was with Yosef, and he was a successful man, and he was in the house of his Egyptian master. (39:2)
The Posuk seems to be indicating that because Yosef was successful, he was therefore in his Egyptian master’s house. His success was due to his skillful management of Potiphar's household, so would it not be obvious, apparently, with no need of mention, that he was there? Another difficulty in this Posuk is: There are three parts to the Posuk. Hashem was with Yosef, he was a successful man, and he was in the house of his Egyptian master. There must be a connection to all three!
Another difficulty is, why didn’t Yosef flee the land of Egypt back to his parents, as it seems he was dealing in business for his master and was free to roam around Egypt!
The explanation lies in the Kedushas Levi, who discusses the posuk: So Yosef’s master took him and put him into prison, ..., and he was there in the prison. If anything, this repetition of messages is even more baffling than the previous one. Yosef’s master took him, put him in prison, and he was there in the prison! The answer is: When Hashem causes something to happen to a person which appears bad, he should not resort to physical schemes in response, but rather rest assured in strong bitachon/faith that Hashem will -- without a doubt -- turn things for the better, and free him from oppression. This was the recurrent trait of Nachum Ish Gamzu (letovah), who was helped without fail, because he accepted each predicament with composure and calm.
Yosef could surely have liberated himself from the prison without any problem, but he remained there, confident that the events unfolding were destined for the good. In Potiphar’s house, Yosef clearly perceived his success as flowing directly from Hashem, the ultimate source of bracha, and made no attempt to leave. This is "Hashem was with Yosef." He had in his soul that Hashem is always with him. Therefore, whatever happens is for his good, and there is no reason to flee. By living with Hashem, there is reciprocation from above, and Hashem grants that person great success, as was the case with Yosef. Hence, that is the reason he was in the house of his Egyptian master.
Now of course, Yosef acquired all of his life skills from Yaakov, who transmitted all that he had learned in the Yeshiva of Shem and Ever. We can certainly assume that Yaakov taught Yosef the Holy Name of Hashem which causes the earth to contract and condense a journey. That Name also gives the ability to transcend borders and boundaries. With knowledge like that, Yosef surely could have freed himself and sped off to his long-grieving father, but no, he remained in his Egyptian master’s house, sure in the knowledge of a favourable outcome, which benefitted the entire Klal Yisroel in years to follow. He also had Ruach Hakodesh that he was destined to be the ruler of Egypt and the bread supplier for his family!
In Megillas Esther (2:11) we find a similar scenario: "Every day, Mordechai would walk about in front of the court of the house of the women, to learn of Esther’s welfare and what would be done to her." With his connections, Mordechai could probably have engineered Esther’s escape from the corrupt Persian court. But with the firm knowledge -- shared by both -- that Hashem had caused her to lodge there for the benefit of Klal Yisroel, he merely “inquired of her welfare.” Here too, Mordechai was a Tzaddik and had miraculous power and apparently could have done something to get Esther out of the royal house, but he understood that if Hashem had put her there, it must be a favor to all of Israel, so all he wanted to see was Esther's well being. But at the end it really came out a favor for all of Israel, as Esther saved the Yidden from Haman and the Bais Hamikdash was eventually built with help of Achashveirosh.
This is a powerful lesson in our everyday life, never to push and pursue actions over and above the natural way of doing things. The Gemarra Berachos 64a states: Rabbi Avin HaLevi said: If one forces the moment and attempts to take advantage of an undeserved opportunity, the moment forces him and he is pushed aside. If one yields to the moment and relinquishes an opportunity that presents itself, the moment yields to him.
(Yehuda Z. Klitnick based on the Sefer Tiferes Yaakov Lifshitz)