In The Name of Allaah, The Most Merciful, The Bestower of Mercy.
Shaikh Abdur Razzaaq Al-Badr [hafidhahullaah] said:
One of the Salaf [Pious Predecessors] said, “Indeed a servant commits a sin due to which he enters Jannah, and he performs a good deed due to which he enters the Fire”. It was said [to him], “How [does that occur]?” He said, “He commits the sin and thus does not cease to think about it; fearing [the evil consequences of] it, very unhappy about it, apprehensive, weeps due to it, regretful, feeling ashamed in the presence of his Lord [The Exalted], his head lowered [in submission and humility] in the presence of [his Lord] and sorrowful for [sake of his Lord]. So, this sin becomes a cause of his happiness and success [i.e. due to repentance], and until it becomes more beneficial for him than performing numerous acts of obedience due to the affairs that emanates from it- [i.e. he did not cease to think about it; fearing (the evil consequences of) it, very unhappy about it, apprehensive, weeps due to it, regretful, feeling ashamed in the presence of his Lord (The Exalted), his head lowered (in submission and humility) in the presence of (his Lord) and sorrowful for (sake of his Lord)]- which brings about the slave’s happiness and success; and until that sin [i.e. the regret and repentance that takes place thereafter] becomes a cause for his entry into Jannah; or he performs a good deed and thus does not cease to regard it as a favour to his Lord, becomes proud due to it, [afflicted with] self-regard and self-amazement due to it; presumptuous, and saying, “I did such and such”, so this leads him to self-amazement, pride, boasting and presumptuousness which becomes a cause of his destruction. [End of quote]
Then the Shaikh [hafidhahullaah] said: When a person hears this [i.e. a sin that enters a person into Jannah and a good deed that enters a person into the fire], he is amazed by it, for indeed it is sin that enters a person into the fire and good deeds enter a person into Jannah, so how can a sin be the cause of entry into Jannah and how can a good deed be the cause of entry into the fire?! So, he [i.e. one of the Salaf who made this statement] clarified it.
A slave [of Allaah] sins due to being overcome by shaytaan, but then he hastens towards his Lord [in repentance], because he knows that he has a Lord who forgives and accepts repentance, so he does not stop weeping and turning to Allaah [in repentance, submission etc], increases in the performance of good deeds which he hopes will be a cause of wiping away his sin and thus this becomes a reason for his entry into paradise. On the other hand, a good deed may lead a person to the fire – not due to the good deed itself – due to the [behaviour] of the person after the performance of that good deed, such as being self-amazed and regarding that deed as a favour to his Lord. Allaah said:
يَمُنُّونَ عَلَيۡكَ أَنۡ أَسۡلَمُواْۖ قُل لَّا تَمُنُّواْ عَلَىَّ إِسۡلَـٰمَكُمۖ بَلِ ٱللَّهُ يَمُنُّ عَلَيۡكُمۡ أَنۡ هَدَٮٰكُمۡ لِلۡإِيمَـٰنِ إِن كُنتُمۡ صَـٰدِقِينَ
They regard as favour upon you [O Muhammad] that they have embraced Islam. Say: “Count not your Islam as a favour upon me. Nay, but Allah has conferred a favour upon you, that He has guided you to the Faith, if you indeed are true. [Surah Al-Hujuraat. Aayah 17]
Allaah said: [وَلَوۡلَا فَضۡلُ ٱللَّهِ عَلَيۡكُمۡ وَرَحۡمَتُهُ ۥ مَا زَكَىٰ مِنكُم مِّنۡ أَحَدٍ أَبَدً۬ا – And had it not been for the Grace of Allah and His Mercy on you, not one of you would ever have been pure from sins]. [Surah An-Noor. Aayah 21]
Therefore, neither think that you have done a favour nor be afflicted with self-regard; neither become self-amazed nor presumptuous. This behaviour is from that which will nullify deeds, such as self-amazement, presumptuousness and show off. This behaviour might occur after the performance of a good deed; for example, it may be an act of obedience which Allaah has enabled you to perform or an affair related to knowledge bestowed on you, or memorisation of [religious texts]; so the person becomes presumptuous, shows off and becomes self-amazed, which becomes a cause of entry into the fire – and Allaah’s Refuge is sought.
There is a beneficial reminder in this affair and that is when a person commits a sin, he should hasten towards repentance, turn to Allaah in repentance- with complete submission, obedience and humility, and he should know that he has a Lord Who forgives. He should be eager to meet Allaah on the Day of Judgement without the burden of his sins. If he is favoured with [the ability to perform] an act of obedience or favoured with some knowledge, he should acknowledge Allaah’s Favour as well as acknowledge that he has great shortcomings, because when a person is attentive to these affairs, it will repel self-amazement. If a blessing is bestowed on a person, firstly he should know that it from Allaah; then he busies himself in thanking Allaah, and then knows that he has many shortcomings, because this will repel self-amazement from him.
[Source: An Excerpt from Sharh Al-Waabilus Sayyib. Lesson 1. By Shaikh Abdur Razzaaq Al-Badr (hafidhahullaah) https://al-badr.net/sub/371. Slightly paraphrased]