When God speaks in Job 38-41, He doesn't seem to answer any of Job's questions. He doesn't tell Job why this is happening. He doesn't tell Job what (if anything) he did to deserve this. He doesn't tell Job why, in general, the righteous sometimes suffer and the wicked sometimes prosper. On the surface what God says has nothing to do with anything Job wants to know. But only on the surface.
God begins in Job 38 by challenging Job to answer some questions. Remember, the last thing Job said was to brag that if he had an indictment from God he would wear it like a crown, march in like a prince, and tell God what's what. (31:35-37) So God says, Ok, I'll ask you some questions and you give me the answers if you know so much. He then asks Job where he was during creation, how the sea was kept in its bounds, and if he could make sure the sun dawned properly, on time, every morning. He asked about the deeps, where light lived, and where God kept the stores of snow and hail. How were the stars kept in their courses? Can Job command the storms? Does Job know anything about the wild animals and how they live?
These obviously rhetorical questions (very sarcastically asked) all have as their answers "I don't know". But on a deeper level, they also imply that God does know. 'Job, you can't do these things, don't understand these things, and can't control these things, but I do understand and can control and order these things,' God seems to be saying. Essentially, God is telling Job to have faith: 'You can't understand it and can't control it, but I can, and I'm on watch. Trust Me.'
Then, after Job's unsatisfactory response in 40:3-5, God begins a second speech which primarily deals with two great beasts which man can't begin to control, but which are small before God. He states His point when speaking of Leviathan in Job 41:10-11 "No one is so fierce that he dares to stir him up. Who then is he who can stand before me? Who has first given to me, that I should repay him? Whatever is under the whole heaven is mine." Unfortunately, the Hebrew in vs 11a -- "Who has first given to me, that I should repay him" -- is very difficult. But all of the various translations have the same underlying idea, that God owes no one anything. This answers Job's questions about why bad things have happened to him despite his prayer being pure and there not being violence in his hands (16:17) but also answers the friends' insistence that God always, and only, rewards the righteous and punishes the wicked (e.g. Bildad in 8:13, 20-21). Being righteous does not earn anyone a reward. If you give all you have and all you are to the Lord, is He obligated to you? No, because "Whatever is under the whole heaven is Mine." He already owned you (and me) and everything you have, so He didn't gain anything by your righteousness, and therefore owes nothing. Likewise, unrighteousness does not hurt the Lord in any way and is not therefore owed punishment. (God does promise ultimate rewards for righteousness and punishment for the wicked in eternity, but He does those things because of who He is and what He has decided to do, not because He owes us anything one way or the other. He doesn't do those things out of obligation to us.) God is telling Job that He can do whatever He wants and He does not have to answer to Job, nor is He in anyway constrained by Job's actions.
Wow, that seems kind of harsh, doesn't it? Kind of scary? Maybe disheartening? But put these two ideas together: the same God who has just said He can do whatever He wants without any reference to man at all has also been spending these two speeches telling Job that He is in control and He knows what is going on and that Job should trust Him. In other words, despite owing Job nothing, God has a plan for him and is making sure that it all works out. That is pretty much the definition of grace and the motivation behind grace is love. I believe God's speeches might be summed up this way: 'Job, there is a plan at work which you can't understand, but I'm in control and I'll make sure it all works out because I love you.'
Does Job get the answers he wanted? No, but he gets a better answer.