The jail that Joseph was in charge of was the place where royal prisoners were held. Two of those were the Pharaoh's butler and the baker. Now, Joseph's relationship with them was a strange one. It is said in verse 4 that the captain of the guard (who was probably Potiphar) gave Joseph the specific responsibility for the security of these two dignitaries. Somewhat strangely, it says that Joseph "served" them.
Apparently Potiphar had determined that Joseph was indeed as trustworthy as he had originally supposed (still he left him in prison... probably to keep his wife pacified). But, though still incarcerated, Joseph was becoming acquainted with more influential people and was indeed continually being groomed for leadership. God was in control.
God gave revelatory dreams to the butler and baker one night, and, they awakened disturbed in their heart by those dreams. It's pretty amazing that Joseph was so interested and attuned to the spirit of his fellow inmates that he immediately asked about their sadness. Doesn't that seem strange? I can't imagine that any warden in any prison anywhere else would be as sensitive to the emotions of his prisoners. "Good morning! Why the long faces fellas?" Fascinating man, this Joseph.
Well, those guys were sad because they didn't know what their dreams meant. But, they hadn't long to wait. They were in the right place with the right man to get an answer from God. The butler was going to be relieved; the baker, devastated.
The butler dreamed a dream about three branches with grapes which showed him being restored to his office in three days. The baker dreamed a dream about three baskets of bread which showed him being hanged in three days.
Now, most importantly in this story, Joseph had expressly requested from the butler that a kind word about him be spoken to the king so he might escape from his incarceration. But, ungratefully (yet providentially), the butler forgot.
God was saving that card for just the right moment; a moment when the Pharaoh would need a dream interpreted for him... and the world would need a leader with God's wisdom in his heart. That man: Joseph!