Joseph is still in prison when the Pharaoh's butler (who is apparently more like a wine steward) and baker are thrown in with him. The captain of the guard puts Joseph in charge of both of them.
One night, each of them has a dream which causes him to be sad. Joseph sees them in the morning and asks why their faces are so long. They explain about the dreams, and how they have no idea what they mean. Joseph has learned something in Egypt: people never want to hear about your dreams, but they are more than happy to tell you about their own, especially if they think you can interpret them. He asks about them.
The wine steward says he dreamed he was back at Pharaoh's house in the vineyard, standing in front of three banches heavy with grapes. He pressed the grapes right there and gave the juice to Pharaoh. Joseph says within three days, Pharoah will lift up thine head, and return thee unto thy place (v.13). He asks the steward to remember him when he's restored and get him out of prison.
The baker loves Joseph's interpretation, so he tells him about his dream: he was carrying three baskets on his head. There were some baked goods in the topmost one, but the birds ate them. Joseph's interpretation is less cheery: within three days shall Pharaoh lift up thy head from off thee, and shall hang thee on a tree; and the birds shall eat thy flesh from off thee (v.19).
It just so happens that three days hence is Pharaoh's birthday. He does indeed put the steward back in charge of the wine, and hang the baker. In a perfect example of gratitude being the shortest-lived emotion, the steward doesn't hold up his end of the bargain.
It's two years later and Pharaoh dreams he's by the river when suddenly seven fat cows emerge and start grazing in a meadow. Then seven lean cows emerge and mingle with the first ones. The lean cows eat the fat cows, at which point Pharaoh awakes, no doubt in a cold sweat, worried about cannibalistic bovines and the risk of Creutzfeldt-Jakob now.
He then has a second dream: one stalk of corn produces 7 plump ears then seven thin ears. The thin ears devour the plump ears. He wakes up again, no doubt wondering what the hell is going on with his crops.
In the morning, he calls all the magicians and wise men of Egypt to him, and tells them about his dreams, but no one has any idea what they could mean, and Freud won't be born for another 5000 years or so. The wine steward finally remembers Joseph. Pharaoh sends for him and tells him what happened.
Joseph says that the seven fat cows and seven ears of corn represent seven years of plenty. The lean cows and thin corn represent seven years of famine. Now, I'm thinking the magicians and wise men all knew this too, but didn't want to take the risk of incurring Pharaoh's wrath. Fortunately, Pharaoh doesn't send him straight back to prison, but takes his advice: hire a man to save one-fifth of the harvest for the next seven years and store it for the famine. Pharaoh not only hires him, he makes him his second in command. He gives him a ring and nice clothes and other jewellery. He even gives him his own chariot. He gives him an Egyptian name, Zaphnath-paaneah, and a wife, Asenath.
Joseph goes out for the next seven years and does his job of storing food in the cities. He also finds time to father two sons, Manasseh and Ephraim. Then the good times come to an end.
The seven years of famine begin, and only Egypt has bread. The people come to Pharaoh for aid and he sends them on to Joseph. The famine is world-wide and Joseph sells the stuff he saved to all comers. No doubt at a steep premium. No mention is made of what happens to those who can't afford it, the only word used is sell.
So what exactly is the purpose of this famine? And has anyone else noticed that god was pretty crafty in his wording of his post-flood promise? He hasn't killed every living thing by drowning, it's true, but he has since rained down fire and brimstone and caused the crops to fail. But hey, we got rainbows!