67 Before I was afflicted I went astray, But now I keep Your word. 71 It is good for me that I have been afflicted, That I may learn Your statutes. 75 I know, O LORD, that Your judgements are right, And that in faithfulness You have afflicted me.
(from Psalm 119)
Why does God afflict His children? He does so because they have drifted from the path that leads to life; they have turned aside from the lifestyle that evokes many blessings from God. What is that path? What is that lifestyle? It is a life lived in obedience to God's commandments (His Law).
The Bible says: "Sin is the transgression of the Law." The Bible says: "Jesus came to save His people from their sins."
When we find ourselves afflicted, then it is a good thing to allow the Holy Spirit to shine the light of God's Law onto our activities. Where we see deviations from, or violations of, God's Law, then we need to "repent" (call our sin, "exceeding sinful") and received the gift of forgiveness. We then need to ask the Holy Spirit to write God's Law upon our heart, so that we fall in love with the right way of living, and learn to hate every sinful way. This process the Bible calls sanctification.
God's afflictions, then, are a rod of sanctification. This is why he tells us (Hebrews 12:4-14):
4You have not yet struggled and fought agonisingly against sin [transgressing the Law of God], nor have you yet resisted and withstood to the point of pouring out your [own] blood. 5And have you [completely] forgotten the divine word of appeal and encouragement in which you are reasoned with and addressed as sons? My son, do not think lightly or scorn to submit to the correction and discipline of the Lord, nor lose courage and give up and faint when you are reproved or corrected by Him; 6For the Lord corrects and disciplines everyone whom He loves, and He punishes, even scourges, every son whom He accepts and welcomes to His heart and cherishes. 7You must submit to and endure [correction] for discipline; God is dealing with you as with sons. For what son is there whom his father does not [thus] train and correct and discipline? 8Now if you are exempt from correction and left without discipline in which all [of God's children] share, then you are illegitimate offspring and not true sons [at all]. 9Moreover, we have had earthly fathers who disciplined us and we yielded [to them] and respected [them for training us]. Shall we not much more cheerfully submit to the Father of spirits and so [truly] live? 10For [our earthly fathers] disciplined us for only a short period of time and chastised us as seemed proper and good to them; but He disciplines us for our certain good, that we may become sharers in His own holiness. 11For the time being no discipline brings joy, but seems grievous and painful; but afterwards it yields a peaceable fruit of righteousness to those who have been trained by it [a harvest of fruit which consists in righteousness--in conformity to God's will in purpose, thought, and action, resulting in right living and right standing with God]. 12So then, brace up and reinvigorate and set right your slackened and weakened and drooping hands and strengthen your feeble and palsied and tottering knees, 13And cut through and make firm and plain and smooth, straight paths for your feet [yes, make them safe and upright and happy paths that go in the right direction], so that the lame and halting [limbs] may not be put out of joint, but rather may be cured. 14Strive to live in peace with everybody and pursue that consecration and holiness without which no one will [ever] see the Lord.