The famous book ‘Ar-Rooh’ (The Soul) by Imam ibn Al-Qayyim has a chapter exclusively on punishment in the grave, which begins with the words:
"One might ask: what are the reasons behind some people being punished in the grave? There are two answers, a detailed one and a concise one. The concise answer is they get punished because of their ignorance concerning Allah, their disobedience to Him and committing what He prohibited; Allah will not punish a soul which knew Him, loved Him, adhered to what He commanded and refrained from what He prohibited, nor will He punish the body in which such a soul was. The punishment in the grave and in the Hereafter is an indication of the Wrath of Allah upon His slave; the one who makes Allah angry in this life and does not repent, and dies while in that state will be punished in the grave according to the extent he angered Allah; some will be punished for a short while, and others for a long time.
The detailed answer is what the Prophet informed us about, when he spoke of the two men whom he saw being punished while in their graves; because one of them went around tale-bearing to cause disputes between people, and the other one did not clean or purify himself after urinating; the second one abandoned the obligatory purification, and the first one committed what causes enmity between people by the evil use of his tongue, even if what he was saying was true. In this there is an indication that the one who causes enmity between people by lying and spreading false statements will receive a harsh punishment, and likewise, there is an indication that the one who abandons prayer will receive a harsher punishment than the one who does not purify himself after urinating, because this purification is only a precondition for praying. In another narration, he informed us that one of the two who were being punished, was a person who used to backbite others."
In one of the Ahaadeeth (narrations), the Prophet informed us about a man in his grave who was lashed once with a whip, causing his grave to become full of fire. This was because he prayed a prayer once without being in the state of ablution, and he also once passed by an oppressed person and did not help him, although he could have done so.” [At-Tahaawi]
Imam Al-Bukhari mentioned in his book a narration by Samurah ibn Jundub that the Prophet informed about the punishment of some people, like one who was punished because he used to lie and his lies became widespread among people; another used to memorize the Quran yet would sleep all night without praying, and would not apply its teachings during the day; a third group of people were those who committed adultery and fornication; another ate up the money earned from Riba (usury and interest). The Prophet informed about all these types of people as he was made able to see them being punished in their graves.
In another Hadeeth, the Prophet told us about some people whose heads are smashed with a rock in their graves because they slept through prayers and never performed them. He also told us about those who are forced to eat Zaqqoom (a plant in Hell) because they did not pay their Zakah (obligatory charity); others have to eat foul rotten meat because they used to commit adultery and fornication; others whose lips are clipped because they used to cause dispute between people during times of trials.
The Prophet also told us about a man who took a garment from the war booty without informing the Prophet of this, and how that resulted in the garment becoming fire which burnt him while he was in his grave.This is why the Prophet taught us supplications seeking refuge from the punishment of the grave to warn and guard us from it.