First of all, I'd like to state that this is a Hasan (fair) hadith, meaning it is not Sahih (authentic) because its isnad (chain of narration) contains 1 or more narrators whose memory weren't considered good enough to make this hadith Sahih.
Allah's Mercy is vast and promises mercy to the believers.
Ordain for us what is good in this life and the next. Indeed, we have turned to You ˹in repentance˺.” Allah replied, “I will inflict My torment on whoever I will. But My mercy encompasses everything. I will ordain mercy for those who shun evil, pay alms-tax, and believe in Our revelations.
In Islam, those who have a mustard seed of faith in their hearts will eventually go to heaven (because of Allah's mercy) and will not abide in hell forever if they were in hell in the first place.
Sahih Bukhari 6560
Narrated Abu Sa`id Al-Khudri: Allah's Messenger (ﷺ) said, "When the people of Paradise have entered Paradise, and the people of the Fire have entered the Fire, Allah will say. 'Take out (of the Fire) whoever has got faith equal to a mustard seed in his heart.' They will come out, and by that time they would have burnt and became like coal, and then they will be thrown into the river of Al-Hayyat (life) and they will spring up just as a seed grows on the bank of a rainwater stream." The Prophet (ﷺ) said, "Don't you see that the germinating seed comes out yellow and twisted?"
We have to believe and trust in the mercy of Allah. If a person dies as a believer even while in a state of sinning (May Allah protect us from that), their ultimate destination is Jannah even if they have to go to hell first.
The Infinitely Merciful and the Question of Hellfire
Dying in a State of Disbelief
But if a person dies in a state of disbelief (whether they were once believers or not), meaning committing a sin that takes them out of Islam such as believing in Shirk, then, unfortunately, all the good deeds they committed in this life will not benefit them in the here-after:
As for the disbelievers, their deeds are like a mirage in a desert, which the thirsty perceive as water, but when they approach it, they find it to be nothing. Instead, they find Allah there ˹in the Hereafter, ready˺ to settle their account. And Allah is swift in reckoning.
Do Non-Muslims Get Rewards For Doing Good Deeds?