All perfect praise is to Allah (SWT), The Lords of the Heavens. I testify that there is none worthy of worship besides Allah and that Prophet Muhammad (PBUH) is His slave and Messenger, may Allah exalt his point out as nicely as that of his family and all his companions.
Imaam ibn Al-Qayyim, may Allah have mercy upon him, dedicated a full chapter in his famous book ‘Ar-Rooh’ (The soul) for this issue, and he began it by stating: "One might ask: what are the reasons behind some people being punished in the grave? There are two answers, a detailed one and a summarized one:
The summarized reply is they get punished due to the fact of their lack of information regarding Allah, their disobedience to Him and their committing what He prohibited; Allah will no longer punish a soul which knew Him, cherished Him, adhered to what He commanded and refrained from what He prohibited, nor will He punish the physique 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 did now not repent, and dies whilst in that kingdom will be punished in the grave according to the extent he angered Allah; some will be punished for a quick while, and others for a long time.
The comprehensive answer is what the Prophet Muhammad (PBUH) 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 was not cleaning nor purifying 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 reasons enmity between humans via mendacity and spreading false statements will obtain a harsh punishment, and likewise, there is an indication that the one who abandons prayer will get hold of a harsher punishment than the one who does now not purify himself after urinating, because this purification is only a precondition for praying. In another narration, Prophet Muhammad (PBUH) informed us that one of the two, who were being punished, was a person who used to backbite others."
In one of the Hadith, the Prophet Muhammad (PBUH) 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).
Imaam Bukhaari, may Allah have mercy upon him, mentioned in his book a narration by Samra ibn Jundab (RA), may Allah be pleased with him, that the Prophet Muhammad (PBUH) 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; the third group of people as those who committed adultery and fornication; another ate up the money earned from Riba (usury and interest). The Prophet Muhammad (PBUH) informed about all these types of people as he was made able to see them being punished in their graves.
In another Hadith, the Prophet Muhammad (PBUH) told us about some people whose heads are smashed with a rock in their graves because they slept through prayers and never performed them. Prophet Muhammad (PBUH) 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' lips are clipped because they used to cause dispute between people during times of trials.
The Prophet Muhammad (PBUH) also told us about a man who took a garment from the war booties without informing the Prophet Muhammad (PBUH) of this, and how that resulted in the garment becoming the fire which burnt him while he was in his grave.
The punishment in the grave is the end result of the sins dedicated by means of the heart, the eyes, the ears, the mouth, the tongue, the stomach, the private parts, the hands, the feet, and the body as a whole.
All of the above-mentioned categories of human beings and others like them will be punished in their graves on account of their crimes, each in accordance to how plenty he committed. He will be punished as a result until he repents to Allah and Allah forgives him due to his repentance, or due to His general mercy upon the Muslims.
How to avoid it
Imaam ibn Al-Qayyim, may Allah have mercy upon him, said: "In general, refraining from what causes punishment in the grave is the best way to avoid being punished. One desires to take a seat for a while earlier than he goes to mattress and maintain himself accountable for what he received at some stage in that day and what he lost, then renew his honest repentance to Allah and sleep after having repented, and when he wakes up, be determined not to commit the identical sins again. He does this each and every night; if he dies that night he would die after having repented, and if he would wake up, he would wake up glad because he was once delayed and given a danger to try hard in properly deeds to make up for what he neglected out on. There is nothing better or more beneficial than such a way of going to sleep, especially if he follows this accountability with mentioning Allah and follows the recommendations of Sunnah in how to go to sleep and supplicate until he falls asleep. The one whom Allah wills well for, He will guide him in doing so."
In detail, we remind people with the hadith of the Prophet Muhammad (PBUH) which inform us about some ways which will rescue us from punishment in the grave:
Salman al-Farsi, may Allah be pleased with him, narrated that the Prophet Muhammad (PBUH), said: “Ribaat (guarding the front lines of the Muslim army) for one day is better (in reward) than fasting for a full month and praying during its nights; and the one who dies while in that position will have the reward of the deeds he was doing to continue (until The Day of Resurrection) and he will be saved from the punishment of the grave." (Muslim).
Amr (Amru) ibn Maadi bin Yakrib al-Zubaidi al-Madhhiji, may Allah be pleased with him, narrated that the Prophet Muhammad (PBUH) said: “The martyr has seven qualities which Allah grants him: he will be forgiven with the first drop of his blood, he will see his seat in Paradise, he will be rescued from the punishment of the grave, he will be rescued from the Great Horror (on the Day of Resurrection), he will wear a (special) garment of faith, he will marry seventy-two women of Paradise, and he will be allowed to intercede for seventy of his family.” (Ibn Maajah). In another narration Prophet Muhammad (PBUH) said: “The crown of dignity will be placed on his (i.e., the martyr's) head, each corundum (of which) is better than this world and all that it contains; he will have seventy-two wives from the ladies of Paradise, and he will be allowed to intercede for seventy of his family.” (At-Tirmidhi).