Joseph was put down a well by his half-brothers to keep him there until they sold to the Midianite merchants, who then on-sold him to Egyptians as a slave.
Joseph's brothers had meant to harm Joseph but God protected him as He wanted Joseph to be in Egypt so that he could one day save his family, in accordance with the dreams God had given Joseph.
As Jacob had tricked his own father Jacob's own sons ticked him.
Joseph was about seventeen when he had his dreams and about thirty years of age when he started to be second in charge in Egypt including ruling over the family in Egypt.
Joseph's Sold by His Brothers
12 Now his brothers had gone to graze their father's flocks near Shechem, 13 and Israel said to Joseph, "As you know, your brothers are grazing the flocks near Shechem. Come, I am going to send you to them."
"Very well," he replied.
14 So he said to him, "Go and see if all is well with your brothers and with the flocks, and bring word back to me." Then he sent him off from the Valley of Hebron.
When Joseph arrived at Shechem, 15 a man found him wandering around in the fields and asked him, "What are you looking for?"
16 He replied, "I'm looking for my brothers. Can you tell me where they are grazing their flocks?"
17 "They have moved on from here," the man answered. "I heard them say, 'Let's go to Dothan.'"
So Joseph went after his brothers and found them near Dothan. 18But they saw him in the distance, and before he reached them, they plotted to kill him.
19 "Here comes that dreamer!" they said to each other. 20"Come now, let's kill him and throw him into one of these cisterns and say that a ferocious animal devoured him. Then we'll see what comes of his dreams."
21 When Reuben heard this, he tried to rescue him from their hands. "Let's not take his life," he said. 22 "Don't shed any blood. Throw him into this cistern here in the desert, but don't lay a hand on him." Reuben said this to rescue him from them and take him back to his father.
23 So when Joseph came to his brothers, they stripped him of his robe—the richly ornamented robe he was wearing— 24 and they took him and threw him into the cistern. Now the cistern was empty; there was no water in it.
25 As they sat down to eat their meal, they looked up and saw a caravan of Ishmaelites coming from Gilead. Their camels were loaded with spices, balm and myrrh, and they were on their way to take them down to Egypt.
26 Judah said to his brothers, "What will we gain if we kill our brother and cover up his blood? 27 Come, let's sell him to the Ishmaelites and not lay our hands on him; after all, he is our brother, our own flesh and blood." His brothers agreed.
28 So when the Midianite merchants came by, his brothers pulled Joseph up out of the cistern and sold him for twenty shekels of silver to the Ishmaelites, who took him to Egypt.
29 When Reuben returned to the cistern and saw that Joseph was not there, he tore his clothes. 30 He went back to his brothers and said, "The boy isn't there! Where can I turn now?"
31 Then they got Joseph's robe, slaughtered a goat and dipped the robe in the blood. 32 They took the ornamented robe back to their father and said, "We found this. Examine it to see whether it is your son's robe."
33 He recognized it and said, "It is my son's robe! Some ferocious animal has devoured him. Joseph has surely been torn to pieces."
34 Then Jacob tore his clothes, put on sackcloth and mourned for his son many days. 35 All his sons and daughters came to comfort him, but he refused to be comforted. "No," he said, "in mourning will I go down to the grave to my son." So his father wept for him.
36 Meanwhile, the Midianites sold Joseph in Egypt to
Potiphar, one of Pharaoh's officials, the captain of the guard.