The Pharaoh's baker and butcher was in the prison with Joseph and they told him about their dreams
The Pharaoh wanted to get rid of a dream he had every night and Joseph was released from his imprisonment to help. The Pharaoh's dream was about seven healthy cows and healthy ears of wheat. Then the exact opposite happened; seven unhealthy cows and ears of wheat. Joseph interpreted this as a sign.
His brothers came to Egypt to trade with the Pharaoh, who was now Joseph, but his brothers didn't know it was him.