The merchant who bought Joseph sold him to an officer Potiphar. Potiphar worked for Pharaoh, the king of Egypt. Joseph was treated very favorably, he put all his affairs in Joseph's care. After Joseph served his master faithfully.
Joseph was falsely accused of doing something wrong. His master, without inquiring into the matter, shut him up in prison. God was with Joseph in the prison, as He had been while Joseph was ruling over Potiphar's household. He caused the keeper of the prison to put trust in him, so that he had the whole care of the other prisoners, and of all that was done there.
Two of these prisoners, chief servants of Pharaoh, dreamed strange dreams, and God gave Joseph wisdom to interpret them. He told one of them that his dream signified that in three days he should be taken out of prison and hanged, the other prisoner's dream signified that in three days he should be released and restored to favor. And he begged this one, after he should be set at liberty, to try to get him also out of prison. But when the man got out of prison, he thought no more about Joseph for two whole years.
The man now worked Pharaoh. The king had two dreams that made him unhappy. None of his wise men could tell him the meaning of his dreams. He dreamed that seven fat cattle were feeding in a meadow and that seven lean ones came and ate them up. Again he dreamed of seven ears of good corn on one stalk, and that seven bad ones sprang up and ate them. So when no one could tell him what these dreams meant, the chief servant remembered how Joseph had explained to him his dream while in prison.
He told the king, who immediately sent for Joseph out of prison. He told his dreams to Joseph and asked him what they meant. Joseph told the king that in his dreams God had showed him what He was about to do. He was going to give Egypt seven years of plenty, and after them seven years of famine. He advised Pharaoh to find an honest person who would rule over the land of Egypt, with officers under him, to store up enough corn during the years of plenty to supply them in the years of famine.
Pharaoh the king thought the advice was good, and that no one was so fit as Joseph to do all this so he made him ruler. Joseph stored up the corn, so that, when the famine came, other countries went to Egypt to buy food.