In Genesis 37 we begin the historical account of Israel's son, Joseph.
Joseph was the second to youngest son of Jacob (Israel, as was the new name given him by God).
Joseph had a dream that his brothers would bow to him, and they mocked him and hated him for that, and because Israel loved Joseph so much. Joseph was the first born of Israel's wife Rachel. This was the love of Israel's life, the woman he fell in love with before he was tricked into marrying her sister, Leah (Gen 29). He worked another 7 years for Laban, Rachel's father, even after working 7 years already and being tricked into marrying Leah, because he loved Rachel so much. This is why he was so fond of Joseph.
Joseph's brothers hated him so much that they conspired to kill him! When they had the opportunity, they decided not to kill him but instead sold him into slavery to a travelling merchant caravan, who took him to Egypt and sold him there to an Egyptian officer, Potiphar.
Now, why did God allow that when Joseph loved and obeyed God? (Gen 39:9 shows an example of Joseph refusing to sin)
If we read on, we see that Joseph ends up in prison, being framed by Potiphar's wife, and he ended up interpreting dreams of the Pharaoh.
God knew that a famine was coming to the land, and he put Joseph in a position to prophesy about it and forewarn the Pharaoh, saving Egypt from starvation. But, most importantly, He saved Joseph and all of Israel's family with that sequence of events leading Joseph to be in a position to make that prophecy.
It may seem awful that Joseph had to go through that, but it saved him and his whole family in the long run.
God also put them there to make a great nation out of them...
Gen 46:3 And he said, I am God, the God of thy father: fear not to go down into Egypt; for I will there make of thee a great nation: 4 I will go down with thee into Egypt; and I will also surely bring thee up again: and Joseph shall put his hand upon thine eyes.
God made a nation of millions from 70 people by bringing them into Egypt. (Gen 46:27 shows us there were a total of 70 that moved to Egypt, and Numbers 1 shows that there were over 600,000 men of military age which means, when we extrapolate the likely number of them all including women, children, and elderly, there was likely 2 or 3 million total)
It was all in God's plan.
So, why then did God allow the Egyptians to enslave the next generations of Israelites while they were there?
Josh 24:14 Now therefore fear the LORD, and serve him in sincerity and in truth: and put away the gods which your fathers served on the other side of the flood, and in Egypt; and serve ye the LORD.
They turned against God and worshipped the false gods of Egypt! Then, when the next Pharaoh came along and feared the Israeli's growth and strength, and enslaved them, they were left without Gods protection because of their turning away from Him!
I am certain that if they hadn't done that God would not have allowed them to be enslaved but would have caused it to still be a place where Israel grew into a great nation in a different way.
So the answer to the question is; God sent Joseph into Egypt so that he could end up being the right hand man of Pharaoh and save Israel from famine as well as making a great and huge nation of them in just a few hundred years. They were allowed to be enslaved because they turned to the false gods of Egypt and were left outside of God's protection because of that.
The lesson in all this is; If you love God and trust Him, and are following Him, then never doubt what is happening in your life even if it seems really bad at the time (like Joseph being a slave and a prisoner, but he never lost faith). All things work together for good to those who love God ... according to His purpose (Rom 8:28). So if you love God and are obedient to Him and find yourself in a place that doesn't seem like it's much of a blessing, just remember that you never know what God has planned for the future and where He might be leading you!