Early in the Book of Job it is made clear that Job’s afflictions were not punishments for sin. Clearly the Lord used the afflictions for purifying, sanctifying purposes, but they were not actually punishments.
Believers are often unaware, as was Job, of the hidden cause and purpose of their trials and experiences (see John 13.7). Job could not foresee how much this affliction would, in the end, benefit him and add to his spiritual blessing. He could not foresee the time when, with his great temporal influence and fortunes restored, he would be an altogether greater influence for the true God, with a doctrinal understanding and experimental faith infinitely stronger than before.
The phrase sons of God certainly refers to angels here, for this interview is presented as if it was in heaven in the presence of the Lord and not on earth. In Genesis 6:2, the same phrase is used to refer to the godly line among men, and we must determine from the context what is meant.