God is so incredibly pure and good, that He takes the most horrible and terrifying circumstances that come about because of sin in the world; and turns it around to accomplish good. In fact, the good that He accomplishes by turning sin around is so good that it often overshadows the bad altogether.
Simply, when we are in God, He doesn't say nothing bad will ever happen to us, on the contrary, He promised persecution (Matt 10:22). But what He did promise is this: That He will never leave us.
God gives us what we call "Shalom" (peace). Shalom doesn't mean nothing bad is happening to you, it rather means you have peace despite whatever is happening because you know that He is in control even when everything feels like it's going wrong. It is the knowing faith that He is one step ahead. That is the kind of peace God gives.
And that is what God did in the story of Joseph's enslavement. He turned Joseph's dire circumstances of being sold into slavery by his brothers into the best thing that could ever happen to his family.
As for you, you meant evil against me, but God meant it for good, to bring it about that many people should be kept alive, as they are today. - Genesis 50:30