In The Name of Allaah, The Most Merciful, The Bestower of Mercy.
Imaam Ibnul Qayyim [rahimahullaah] said:
The Shahaadah [i.e. to testify that ‘None has the right to be worshipped except Allaah] at the time of death wipes away one’s evil deeds. This is because the one who utters it at the time of departure from this world does so with certainty and knowledge of its true meaning. All his desires [i.e. desire for the worldly life] come to an end and his rebellious soul [becomes] mild and submissive after being in [a state of] refusal and disobedience- a [state of] approval after turning away and a state of humility after being held in high esteem. The [heart’s and the soul’s] eagerness for the worldly life and its merits departs and distances [from everything] whilst in the presence of its Lord, Originator and true Protector; humble in the presence of its Lord, hopeful of pardon, forgiveness and mercy.
Belief in the Oneness of Allaah-[i.e. the belief in the Oneness of Allaah’s Lordship (that Allaah alone is the creator, provider and the one in control of all the affairs of the universe); and that Allaah alone has perfect Names and Attributes and that none is similar to Allaah; and that Allaah alone has the right to be worshipped]-frees and distances the soul from that which leads to Shirk [polytheism] and manifests the reality of its falsity. Therefore, those wishes which the soul busied itself with [in the worldly life] ceases [at the time of departure from this world] and all its concern is [directed towards] the One it is certain to meet [i.e. Allaah].
The person [who is blessed with the ability to utter this testimony of faith] focuses his attention completely on Allaah- turns to Allaah with his soul and desire and submits to Allaah alone, inwardly and outwardly. The person’s hidden affair [i.e. the heart and soul] and outward affair [i.e. what he proclaims] testify to the same thing, so he sincerely says: “None has the right to be worshipped except Allaah.” His heart is freed from attachment to other than Allaah -all the worldly [desires] leaves the heart because he is about to stand in Allaah’s presence. The intensity of his desire [for the worldly life] is extinguished and the heart is filled with the [desire for] the afterlife, and thus it becomes the attention of his eyes and the worldly life is forgotten. [When] this sincere testimony is his last deed, it purifies him from sins and thus Allaah enters [him into a noble position]. That is because he meets Allaah with a truthful and pure testimony, whose outward manifestation is in agreement with [what is hidden in the heart and soul].
If a person made this [type] of testimony during the time of well-being [i.e. whilst residing in the worldly life], he would not have placed great importance on worldly desires, rather he would have attached himself to Allaah alone; but [the fact of the matter is that] he made this testimony [in the worldly life] with a heart filled with desires, love of life and its means of subsistence.
[Source: An Excerpt from ‘Al-Fawaa-id. Page: 91-92. slightly paraphrased]