This question comes up a lot in cultures that embrace the idea of an all-knowing, all-powerful benificent force underpinning the universe. From what we know of human love and relationships, we assume that a loving deity will deal kindly with us: giving us everything we need; and will at the very worst simply subject us to justice: rewarding our good deeds and punishing our evil. The idea that such a deity would allow unwarranted evil to befall us, depriving us of those things that we most want in life, is abhorrent and causes we who have adopted this religious world-view a great deal of cognitive dissonance. Whatever we did, we think, was surely not bad enough to justify what is happening to us now.
The Book of Job grapples with this question. Possibly one of the oldest books of the Hebrew Bible, Job is reportedly one of the hardest to translate, as though it itself is a translation from an older language. Similar stories can be found throughout the Semitic world, indicating either that the story has an ancient distribution across many peoples. In the story, Satan challenges God to a bet over the righteousness of God's servant, Job. Job is a good man who honors God regularly, and has acquired all of the good things of life for his faith. God gives Satan the power to destroy his life, and Job loses his flocks, his possessions, and his children. In the main section of the book, Chapters 3-33, Job has an argument with three of his friends, maintaining his innocence against their insistence that Job must have done something wrong to deserve his fate. Job maintains his innocence, and laments that he cannot call God to account: "He is not a mere mortal that I might answer him, that we might confront one another in court" (Job 9:32, New International Version).
This is a remarkable statement. Job has not "cursed God," as his wife insists, but is calling into suspicion God's justice. This is something that Job would never have done in his old life. When God returned good for good, it was easy enough for Job to conflate piety and morality: whatever God commanded must have been good, for God was good. In this framework, Job had no need for an independent moral sense; he could outsource this moral independence to God, and simply follow God's will.
Imagine that we lived in a world of perfect justice: where evil always met with evil and good always met with good. In such a world, everyone would be perfect, for moral perfection would be pleasurable. Evil would immediately cause pain, and good works would be like drugs. In such a world, we would all be "good addicts." There would be no need for morality. There would be no need for free will. We would learn to follow the commands of our divine leader whatever they were, all in the hope that we could score some of that "good juice."
Our own world is rather more like Job's. In our world, many saints are born into dire poverty, and many sinners are given every privilege and the opportunity to lead armies on missions of oppression and genocide. We live in a world of injustice: one that stimulates our moral judgment and demands that we walk a difficult path if we are to live in accordance with it. Without this injustice, we would have no chance to choose whether we ourselves were to be just or unjust beings.
At the end of the book of Job, God speaks to Job, saying "Where were you when I laid the earth's foundation? Tell me, if you understand. Who marked off its dimensions? Surely you know! Who stretched a measuring line across it?" (NIV Job 38:4-5). The traditional interpretation of this line and those following it is that God has decided to over-awe Job, and thereby discourage his moral questioning of God's action.
There is another interpretation, however; one which I prefer. What if God's fantastic descriptions of the Earth and its creatures are meant to illustrate his role in creation, not simply his overwhelming power? This would mean that, by describing Creation to Job, God would somehow be bringing Job into the act of creation. Job's suffering itself would have been a creative act. Before, Job was little better than an animal: blindly following the commands of his master. Now, Job is an entity with free will and an independent moral sense like God. By communicating with Job after his ordeal, God is asking for more than Job's faith: He is asking for Job's trust, something that Job had no power to bestow previously. It is fitting that God's direct communication comes only at the end of the story, only after Job becomes worthy of it.