Illnesses can be a test, punishment, or expiation from sins or all three. It depends on the case.
Here's about the expiation from sins:
There are many hadith which explain that sickness expiates evil deeds and wipes out sins. Of these, some are given below:
Abu Hurairah narrates that the Prophet, peace be upon him, said: "When Allah wants to be good to someone, He tries him with some hardship."
Abu Hurairah also reports that Allah's Messenger, peace be upon him, said: "For every misfortune, illness, anxiety, grief, or hurt that afflicts a Muslim -even the hurt caused by the pricking of a thorn - Allah removes some of his sins." Ibn Mas'ud said: "I visited the Messenger of Allah, peace be upon him, while he had a fever. I exclaimed: 'O Messenger of Allah! You have a high fever! ' He said: 'My fever is as much as two among you [might have]. ' I asked: 'Is it because you have a double reward?' He replied: 'Yes, that is right. No Muslim is afflicted with any hurt, even if it is no more than the pricking of a thorn, but Allah wipes off his sins because of it and his sins fall away from him as leaves fall from a tree'."
Abu Hurairah said: "The Prophet, peace be upon him, remarked: 'The example of a believer is like a fresh tender plant; from whichever direction the wind blows, it bends the plant. But when the wind dies down, it straightens up again. (Similarly a believer is tested by afflictions to strengthen his faith and heart, and he remains patient and firm). And an evil person is like a pine tree which remains hard and stiff until Allah breaks it whenever He wills."
Volume 4, Page 1a: Patience During Illness
Anyone suffering from an illness should remain patient, for there is no reward better or more enriching than that reserved for those who endure in patience.
Suhaib ibn Sinan narrated that the Prophet, peace be upon him, said: "How remarkable is the case of the believer! There is good for him in everything, but this is not the case for anyone except for the believer. When the believer receives any good, he is thankful to Allah, and gets a reward. And when some misfortune befalls him, he endures it patiently, for which he is (also) rewarded."
Anas narrates: "I heard the Prophet, peace be upon him, saying: 'Allah says: "When I afflict a servant of mine with respect to his two most beloved things (meaning his eyes), and he endures it patiently, I grant him paradise in return'."
'Ata ibn Rabah related that he heard Ibn 'Abbas say: "Shall I show you a woman of Paradise?"I said: "Yes, indeed." He said: "A black woman came to the Prophet, peace be upon him, and said: 'I suffer from epileptic fits, and because of these, (at times) my body becomes uncovered. Would you invoke Allah, the Exalted One, to cure me of this disease? ' The Prophet, peace be upon him, said: 'If you wish, you can be patient and you will attain Paradise (for this suffering). But if you prefer, I will pray to Allah, the Exalted, to cure you of it?' The woman said: 'I will be patient,' then added: 'I become uncovered (when I have fits), so invoke Allah for me that I do not become uncovered. ' So the Prophet, peace be upon him, prayed for her."
Volume 4, Page 2: To Complain of One's Illness
It is permitted for a patient to complain of his pain and illness to a physician or a friend, provided he does not do so to express his or her anger or impatience. It was mentioned earlier that the Prophet, peace be upon him, said: "My fever is as severe as that of any two of you." Once 'Aisha complained to the Messenger of Allah, peace be upon him, about her headache, lamenting: "O my head." He retorted: "Nay, rather (I should say) O my head!" Likewise it is reported that 'Abdullah ibn Al-Zubair asked his ailing mother, Asma bint Abi Bakr, "How are you feeling now?" She replied: "I am in pain."
A patient should thank and praise Allah, before talking about his distress and complaint
Ibn Mas'ud said: "If one thanks Allah before complaining about his pain or disease, then it is not considered impatience. Indeed, to refer one's complaint to Allah, is quite lawful." Jacob (the prophet), said: "I complain of my distraction and anguish only to Allah." The Prophet Muhammad, peace be upon him, himself prayed: "O Allah! to You I complain of my weakness."
Volume 4, Page 2a: A Sick Person is Rewarded for All the Good Deeds that He Would (usually) Perform in a State of Health
Abu Musa al-Ash'ari reports that the Prophet, peace be upon him, said: "If a servant (of Allah) falls sick or goes on a journey, he (continues to be) rewarded for the good deeds that he used to do when he was healthy or at home."
For more readings: