Praise be to Allah, and peace and blessings be upon the Messenger of Allah. To proceed:
This worldly life is not the place of final reward or punishment. Rather, it is a place of deeds and trials. The complete recompense will be in the Hereafter, where Allah will reward the believers with Paradise and punish the disbelievers with Hell.
By His perfect wisdom, Allah tests His servants so that the sincere believer may be distinguished from others. Allah, the Exalted, says:
"Do people think they will be left to say, 'We believe,' without being tested? Indeed, We tested those before them, so Allah will surely make evident those who are truthful and those who are liars."
There are many great wisdoms behind the trials of this worldly life, including:
Allah tests His servants in many different ways, including:
Allah says:
"We test you with both hardship and prosperity as a trial, and to Us you will be returned."
He also says:
"Know that your wealth and your children are only a trial."
The stronger a person's faith is, the greater their trials may be. Therefore, the Prophets were the most severely tested, followed by those who were most like them in righteousness.
Trials are not necessarily a sign of Allah's anger. Rather, they may be a sign of elevated status if the servant remains patient and seeks Allah's reward.
A hardship may sometimes be a punishment or discipline because of sins, as Allah says:
"Whatever calamity befalls you is because of what your own hands have earned, though He pardons much."
At other times, a hardship may be a mercy from Allah to erase sins or raise a believer's rank, as the Prophet ﷺ said:
"No fatigue, illness, anxiety, sorrow, harm, or distress afflicts a Muslim—not even the prick of a thorn—except that Allah expiates some of his sins because of it."
For this reason, it is not permissible to state with certainty the specific reason behind every calamity. Rather, the believer knows that all of Allah's decrees are based upon wisdom, justice, and mercy.
This worldly life is a place of trials, not the place of final reward. Allah tests His servants through ease and hardship, blessings and difficulties, in order to distinguish the truthful from the false, raise the ranks of the believers, expiate their sins, and call sinners to repentance. The complete reward for deeds will be on the Day of Resurrection, when the people of faith will enter Paradise by Allah's mercy, and the disbelievers will receive their recompense through His perfect justice.
And Allah knows best.