Punishments are what happen to people in return for their evil deeds. Trials are tests which are sent to try people, and people may be tested by good things or by bad.
Concerning punishments, Allah says (interpretation of the meaning):
“Whatever of good reaches you, is from Allah, but whatever of evil befalls you, is from yourself…”
Concerning the phrase “is from yourself”, Qatadah said: “[It means] the punishment is for you, O son of Adam, because of your sin.” Abu Salih said that “whatever of evil befalls you, is from yourself” means “because of your sin, and I [Allah] am the One Who decreed it for you.” (Tafseer Ibn Katheer).
Allah also says (interpretation of the meaning): “And whatever of misfortune befalls you, it is because of what your hands have earned. And He pardons much.”
Ibn Katheer, may Allah have mercy on him, said: “[It means] whatever misfortune happens to you, O people, is because of evil deeds that you have already done, and ‘He pardons much’ refers to evil deeds – He does not punish you for them but He pardons them. ‘And if Allah were to punish men for that which they earned, he would not leave a moving (living) creature on the surface of the earth…’ [Fatir 35:45 – interpretation of the meaning].”
Concerning trials, the Prophet (peace and blessings of Allah be upon him) said: “If Allah loves a people, He tries them, and whoever has patience will have patience, and whoever is anxious will be anxious.” (Reported by al-Imam Ahmad; Saheeh al-Jami’, 1706).
The Prophet (peace and blessings of Allah be upon him) also said: “The extent of the reward will be in accordance with the extent of the trial. If Allah loves a people, He tries them, and whoever is content will have contentment, and whoever is angry will have anger.” (Reported by al-Tirmidhi, 2320; Saheeh al-Jami’, 2210).
The following outlines how one may tell whether a given event is a punishment or a trial:
If the misfortune results from doing an act of obedience to Allah, such as being wounded in jihad, or losing money when migrating for the sake of Allah, or losing a job because of becoming a Muslim or increasing one’s commitment to Islam, then this is a trial. Whoever bears it with patience will be rewarded, but if one reacts angrily then one will earn the wrath of Allah. If the misfortune results from sin, such as sexually-transmitted diseases or illnesses caused by drinking alcohol and taking drugs, and so on, then this is the kind of punishment that comes in this world (as opposed to being deferred until the Hereafter). If the misfortune is connected neither to a good deed nor a sin – such as other kinds of diseases and sicknesses, losing a child, or failing in business – then it depends on the person’s situation. If he is good and devout, it is a trial, and if he is sinful, it is a punishment.
The misfortune may be a punishment to atone for sins, or it may be a trial aimed at raising a person’s status and increasing his hasanat (good deeds/rewards) – this may be determined by whether a person is obedient or disobedient. A person should not praise himself, rather he should criticize himself for his shortcomings and strive to attain perfection. He will benefit from misfortunes in any case, if he has patience and hopes for reward.
And Allah knows best.