His 3 friends who said bad things don't happen to good people were wrong. He was also queried by God for his own view (which was asking why. Or was it self pity?). So what was the lesson learnt?
The point of the book of Job is revealed in the final chapters, in which God answers Job directly, and Job's "comforters" indirectly. From our point of view, God's answer is not very satisfying, in part because God does not give Job a reason for why He allowed him to suffer so horribly!
And that, my friend, is precisely the point of the book of Job.
We easily get sidetracked with peripheral--albeit significant--issues such as
The main point of the book is that God is the potter, and we His creatures are the clay. The clay doesn't tell the potter what to do (see Isaiah 29:15-16, and 45:9 ff.).
Similarly, for reasons known ultimately only to Him, God shapes, molds, and fashions His children in ways which from our finite point of view seem only to hurt us. The truth is, however, God sends us trials, ultimately, to make us BETTER, not BITTER. To switch metaphors, the vinedresser prunes us, the vine's branches(ouch!) , so that we bear more fruit! That is His right. He is the vinedresser (John 15:1,2).
Job was tempted to raise an accusatory finger to God, but in the end he repented in dust and ashes, saying,
In other words, Job stood in awe of God, realizing God is God, and that His thoughts and ways transcended Job's ability to understand (see Isaiah 55:8-9). Job finally realized in chapter 42 that for him to "sit in judgment on God" was unthinkable, as was his expecting God to answer his questions, at least to Job's satisfaction!
God alone is the Judge of all the earth (Genesis 18:25b), and our "job" (no pun intended) as His creatures is to obey, serve, love, and submit to Him with all our heart, soul, mind and strength.
In conclusion, we do well to say, along with Job,
At the end of his ordeal, Job learned his place as a lump of clay: on the potter's wheel.