It states (39:20), בית אל ויתנהו אותו יוסף אדני ויקח אסורים המלך אסירי אשר מקום הסהר, "Yosef's master took Yosef and put him into prison where the king's prisoners are detained." How hard it must have been for Yosef HaTzaddik to be imprisoned there, underground, together with criminals. The supply of food, drink, and other necessities was probably minimal. He was alone, distant from his family, a family of tzaddikim and kedoshim. But there are indications that Yosef was always happy, even in prison. The end of the pasuk states, הסוהר בבית שם ויהי, "and he was there, in prison." These words seem superfluous because where else would he be? Obviously, if Yosef was placed in jail, he would be "there, in prison." The Torah tells us that Yosef was happy to be in jail. It wasn't a b’dieved, a negative experience he had to endure. שם ויהי הסהר בבית, he was in prison with all his heart and soul. He believed that Hashem placed him in prison, so this was the best place for him to be, and that he had a mission to accomplish there.
The Sfas Emes writes, “Yosef was extremely wise, and he was also wealthy. (Potiphar gave Yosef control over everything he owned [see 39:8]). If Yosef wanted, he could have figured out a way to get out of prison. But he believed his imprisonment was Hashem’s will, so he remained and didn’t try to leave.”
Similarly, the Kedushas Levi writes, “Yosef could have tried to free himself from jail, but he didn’t because he trusted in Hashem that everything was for the good.”
Two Midrashim discuss the quality of being loyal to one's boss. One Midrash says that Eliezer (the slave of Avraham) was cursed (ארור), and he became blessed (ברוך) because he served his master Avraham loyally. Another Midrash (Shir HaShirim 1:1) states that Yosef served Potiphar loyally, and in this merit, he was saved from jail.
The Sifsei Tzaddik asked his brother-in-law, the Sfas Emes, "We understand that Eliezer was blessed for his loyal service to Avraham Avinu, but why would Yosef be freed from prison because he served Potiphar loyally? What was special about serving Potiphar?"
The Sfas Emes explained that the Sifsei Tzaddik hadn't translated the second Midrash correctly. The Midrash states, על יוסף לחירות יצא באמונה רבו את ששימש ידי. Although the simple translation באמונה is "loyally," in the context of this Midrash, it means "belief." The Midrash is saying, “Yosef served his master [Potiphar] with the belief that it was bashert, and he was fulfilling Hashem’s will by serving Potiphar. In that merit, he was freed." Yosef believed that Hashem planned that he be a slave to Potiphar, and therefore, he accepted Hashem's will with love. In the merit of his emunah, he was freed from jail.
Yosef believed that he was enslaved in Potiphar's home because this was Hashem's will. An indication of this is the description that the pasuk gives about this period of Yosef's life. It states (39:2), יוסף את 'ה ויהי, “Hashem was with Yosef.” This means Yosef was happy at this time of his life because Chazal (Shabbos 30) say, "The Shechinah only rests on someone who is happy." Also, about this time, in Potiphar's house, it states (39:2), מצליח איש ויהי, "He was a successful man," and the Midrash translates מצליח איש as קפוז גבר, “a dancing person." Yosef was dancing and filled with joy. It is obvious that the joy Yosef had was the outcome of his emunah. He believed that Hashem wanted him to be a slave in Mitzrayim, so why shouldn't he be happy?
Accept Hashem's Will with Joy
Rebbe Elimelech of Lizhenzk and Rebbe Zusha of Anipoli zt'l were once placed in prison, and there was a bucket in their cell for the inmates to use as a toilet. Rebbe Elimelech was very upset because, due to this bucket, they couldn't daven or learn Torah. His brother, Rebbe Zusha, explained that the bucket was, in fact, a reason to rejoice because by refraining from studying Torah and saying tefillos, they were doing Hashem's will, and that is a reason to rejoice. Rebbe Elimelech agreed, and they sang and danced around the bucket.
The angry prison warden rushed in to see what the commotion was about. When he saw them dancing around the bucket, he thought they were celebrating the convenience of having a beis hakisei in their cell. He shouted, "Oh, so this is what you are so happy about!" and removed the pail from the room.