Praise be to Allah, and may peace and blessings be upon the Messenger of Allah. To proceed:
Belief in the Hereafter is one of the six pillars of faith. It includes belief in the Resurrection, the reckoning, Paradise, and Hellfire. Therefore, a servant’s faith is not valid if he denies this pillar or doubts that it will occur.
Allah تعالى says in describing the believers:
﴿AND THEY ARE CERTAIN OF THE HEREAFTER﴾ [An-Naml: 3].
It is established in the hadith of Jibril that the Prophet ﷺ said, when explaining faith:
“That you believe in Allah, His angels, His Books, His messengers, and the Last Day, and that you believe in divine decree, its good and its evil.” Narrated by Muslim.
Allah تعالى has paired belief in Him with belief in the Hereafter in many places, which indicates that belief in the Hereafter is an essential consequence of belief in Allah. He سبحانه says:
﴿BUT RIGHTEOUSNESS IS [THE QUALITY OF] THE ONE WHO BELIEVES IN ALLAH, THE LAST DAY, THE ANGELS, THE BOOK, AND THE PROPHETS﴾ [Al-Baqarah: 177].
The good deeds performed by a disbeliever do not benefit him in the Hereafter, nor are they a cause for him to enter Paradise or be saved from punishment.
The Prophet ﷺ was asked about Ibn Jud‘an, who used to maintain family ties and feed the needy during the pre-Islamic period, and whether that would benefit him. He ﷺ said:
“It will not benefit him, for he never said: ‘My Lord, forgive me my sin on the Day of Judgment.’” Narrated by Muslim.
Imam An-Nawawi رحمه الله explained that what is meant is that the good deeds he used to perform did not benefit him in the Hereafter because he did not believe in the Resurrection and the reckoning.
As for a passing doubt or intrusive thought that occurs to a person without conviction or choice, it is not the same as denying the Hereafter out of belief and rejection. Therefore, a person is not judged to be a disbeliever merely because of passing thoughts or intrusive whispers.
Belief in the Hereafter, the Resurrection, the reckoning, Paradise, and Hellfire is one of the pillars of faith. A person’s faith cannot be valid while he denies or rejects this fundamental principle. Whoever dies upon disbelief in Allah or in something established as one of the fundamentals of faith will not benefit from his deeds in the Hereafter.
And Allah knows best.