Deus ex machina (google it!)
"You intended to harm me, but God intended it for good to accomplish what is now being done, the saving of many lives." Genesis 50.20
Perhaps the biggest historical question of all is how God exercises his sovereignty over creation. If he really does hold the whole world in his hands, how can we explain things that go horribly wrong, like natural disasters? Or how can events that contradict what we think we know about God, like the holocaust?
One explanation is suggested by Joseph's remark above, when he reconciles his brothers after they have sold him into slavery in Egypt and years later they come, cap in hand, seeking relief from famine in Israel only to find that he has become Prime Minister! Magnanimously, Joseph relieves their embarrassment and shame by ascribing it to divine intervention, providing the precedent for St Paul's much later reassurance to suffering Christians that "in all things God works for the good of those who love him" Romans 8.28.
In the same letter Paul admonishes his readers: "Do not take revenge, my dear friends, but leave room for God's wrath, for it is written: "It is mine to avenge; I will repay," says the Lord." (Romans 12.19) Paul's advice rests on the conviction that God intervenes in history to sort things out. So, in the face of inexplicable or unpalatable events, we can trust that God will bring justice - in his own way and in his own time.
But this does not explain why he would allow such injustices to occur in the first place. Is this not where human free will intersects with God's sovereign power? Because God desires an authentic relationship with the creatures he has made in his own image (the implications of which are so huge they must be explored on another occasion...) we must be allowed to make genuine choices, whatever the consequences. Reassuringly, the Bible asserts that God will clean up the mess and he has already acted decisively to that end - in sending Jesus, whose life, death and resurrection is the ultimate expression of "Deus ex machina"!