So, before the random segue into the story of Judah, which seems fabricated specifically to make a point of how arbitrarily cruel God can be, Joseph had been sold and was being carted off to Egypt.
In Egypt, some guy named Potiphar bought Joseph, and he soon noticed that, "the LORD gave him success in everything he did," so Joseph was put in charge of the household, and "the LORD blessed the household of the Egyptian because of Joseph."
He took his job very seriously, so seriously that he turned down the repeated and belligerent attempts of Potiphar's wife to sleep with him. Once she managed to grab is cloak as he was running away from her, and she showed it to Potiphar and told him that Joseph had tried to sleep with her (see, the cloak proves it!). Potiphar is dumb enough to fall for this and has Joseph put in jail, where he is somehow a prisoner and also in charge of all the other prisoners, because the wardens like him so much.
The royal cupbearer (cupbearer? lol.) and baker somehow "offended" the king, and they were thrown in jail. Joseph was put in charge of them. At some point, they both had dreams, and they were totally depressed because there was no one around to interpret them. Joseph says, "Do not interpretations belong to God? Tell me your dreams."
So...interpretations belong to God, so only God can interpret your dreams. But, tell me your dreams, and I'll take a crack at interpreting them. Huh? That makes no sense...
Anyway, the cupbearer's (lol) dream was that there was a vine with 3 branches, and he took the grapes from them and squeezed the juice into the pharaoh's cup and gave it to him. The baker's dream was that he had 3 baskets of bread, but birds kept eating it.
Joseph, with his mighty god-like wisdom, told them the interpretations of their dreams...the cupbearer's dream meant that he would be let out of jail and reinstated as royal cupbearer in 3 days, while the baker's dream meant that the king was going to have him killed in 3 days. And of course, that's just what happened.
The cupbearer was supposed to try to get Joseph out of jail, but he forgot about him. Poor Joseph.