Hazrat Moulana Ashraf Ali Thaanwi (rahimahullah) once mentioned the following:
At the time of death, a person’s heart is overcome by different thoughts. However, only those thoughts are harmful to him which he entertains. As for those thoughts which enter his heart involuntarily (and he does not entertain them), then such thoughts will not be harmful. In regard to thoughts which cross the heart, there is some explanation (as to which thoughts are harmful and will cause one to become sinful).
Nevertheless, the thing that is most detrimental to a person at the time of death is the love for the dunya (world). The reason is that when a person’s heart is overcome by the dunya, and he has (excessive) love for the dunya, then at the time when he is leaving the world – which is the time of death – there is great fear that he should not begin to hate the One who is taking his life and removing him from the dunya (i.e. Allah Ta‘ala), as this hatred for Allah Ta‘ala will cause him to fall into kufr (i.e. reject the decree of Allah Ta‘ala).
An excellent remedy and treatment for this spiritual ailment (of the love for the world) is that one should suppress his love for the world by pondering over the time when one will have to leave the world (i.e. pondering over death, when one will be forced to leave the luxuries of the world and go to the Hereafter). If one does this regularly, then insha Allah, there will be no harm or danger (to his imaan).
Every Muslim understands that the belief of a Muslim is that the dunya is not good for him (as it may cause him to fall into disobedience, and the Hereafter is the actual place of success). However, one should cause this belief to reach the level of conviction, where this thought becomes the overwhelming and dominant thought at all times.
(Malfoozaat Hakeemul Ummat 1/77-78)