Fitnah of the Shaytaan (and his attempts to mislead the individual) at the time of death
Question

Is it true that the Shaytaan may come to a person at the time of death, so that the person will die as a disbeliever even though he did the deeds of the people of Paradise all his life?
Praise be to Allah.Firstly: 
Allah mentions in His holy Book the oath of the accursed Iblees and his threat to the children of Adam to misguide them and lead them astray, and that he will carry on doing that until the Day of Resurrection, and he will not leave any way of filling Hell with the progeny of Adam (peace be upon him) but he will pursue it. 
Allah, may He be exalted, says (interpretation of the meaning):
“(Iblees) said: ‘Because You have sent me astray, surely I will sit in wait against them (human beings) on Your Straight Path.
‘Then I will come to them from before them and behind them, from their right and from their left, and You will not find most of them as thankful ones (i.e. they will not be dutiful to You)’”
[al-A‘raaf 7:16-17]. 
Ibn Jareer at-Tabari said in Jaami‘ al-Bayaan (5/445): 
What this means is: Then I shall come to them from all directions, from the direction of truth and the direction of falsehood; I shall bar them from the truth and I shall make falsehood fair-seeming to them. End quote. 
The Shaytaan tries to exploit weak points. If he finds that a calamity has overwhelmed a believer and that distress has overtaken him, he will interfere in order to spoil his faith, so that he will become one of the people of Hell. 
Undoubtedly the time when death is approaching is a time of distress and hardship, for the agonies of death are extremely difficult to bear. The Prophet (blessings and peace of Allah be upon him) found them very hard to bear, to the extent that he (blessings and peace of Allah be upon him) would say: “Verily death comes with agonies.” Narrated by al-Bukhaari (4449).
When his daughter Faatimah realised the hardship that he was faced with, she said: How hard it is for my father! Narrated by al-Bukhaari (4461)
What is to be expected is that the Shaytaan will never leave the son of Adam alone in this situation, for it is an opportunity for him.
It was narrated that Jaabir (may Allah be pleased with him) said: I heard the Prophet (blessings and peace of Allah be upon him) say: “The Shaytaan is present with any one of you in all his affairs.” Narrated by Muslim (2033).
It was narrated from Abu Sa‘eed al-Khudri (may Allah be pleased with him) that the Prophet (blessings and peace of Allah be upon him) said: “Iblees said: ‘By Your Glory, I shall strive to lead Your slaves astray so long as their souls are in their bodies.’ Allah said: ‘By My Glory and Majesty, I shall continue to forgive them so long as they ask Me for forgiveness.’”
Narrated by Ahmad (10974); classed as hasan by al-Albaani in Saheeh at-Targheeb (1617)
The Prophet (blessings and peace of Allah be upon him) used to ask Allah, may He be exalted, not to allow the Shaytaan to gain power over him at the time of death, so as to teach the Muslims to strive to be safe from the fitnah of the Shaytaan.
It was narrated from Abu’l-Yusr that the Messenger of Allah (blessings and peace of Allah be upon him) used to say in his supplication: “O Allah, I seek refuge in You from being crushed; and I seek refuge in You from falling from a height, drowning, being burned and from senility; and I seek refuge in You lest the Shaytaan gain control over me and confuse me at the time of death; and I seek refuge in You lest I die having turned my back whilst fighting for Your sake; and I seek refuge in You lest I die from a venomous sting or bite.”
Narrated by Ahmad (3/427); Abu Dawood (1552) – he did not comment on its soundness or otherwise; an-Nasaa’i (5531). Al-Haakim said in al-Mustadrak (1/713): Its isnaad is saheeh although they [al-Bukhaari and Muslim] did not narrate it. It was classed as saheeh by al-Albaani in Saheeh Abi Dawood.
It says in ‘Awn al-Ma‘bood (4/287)
“lest the Shaytaan gain control over me and confuse me at the time of death” that is, lest the Shaytaan or one of his helpers do that. It was suggested that the word translated here as “gain control over and confuse” refers to causing confusion in one’s reasoning and religious belief. The meaning is made specific by the words “at the time of death”, because what matters is how one’s life ends.
Al-Qaadi said: That is, lest the Shaytaan confuse me with his evil whispers, which cause feet to slip and create tumoil in the mind.
Al-Khattaabi said: The Prophet (blessings and peace of Allah be upon him) sought refuge with Allah lest the Shaytaan confuse him at the time of death, which refers to the Shaytaan gaining control of a person at the moment of his departure from this world, and misleading him and preventing him from repenting, or preventing him from setting his affairs straight and restore people’s rights, or making him despair of the mercy of Allah, may He be exalted, or making him hate death and prefer the life of this world, so that he is not content with what Allah has decreed for him of dying and moving to the Hereafter. This causes him to have a bad end and to meet Allah when He is angry with him.
It was narrated that the Shaytaan is never more eager to harm the son of Adam than at the time of his death. He says to his helpers: Go after this one, for if he gets away from you today, you will never catch up with him after today.
 We seek refuge with Allah from his evil, and we ask Him to bless us at that critical moment, and to make the best of our days the day we meet Him. End quote.
The fitnah of the Shaytaan at that moment is a severe fitnah, because of what the Muslim is faced with of exhaustion and distress. The Prophet (blessings and peace of Allah be upon him) used to seek refuge with Allah from that in his supplication in every prayer.
It was narrated that Abu Hurayrah (may Allah be pleased with him) said: The Messenger of Allah (blessings and peace of Allah be upon him) said: “When any one of you says the tashahhud, let him seek refuge with Allah from four things. Let him say: O Allah, I seek refuge with You from the punishment of Hell, from the punishment of the grave, from the trials of life and death, and from the evil of the tribulation of the Dajjaal.”
Narrated by al-Bukhaari (1377) and Muslim (588).
Ibn Hajar said in Fath al-Baari (2/319):
Ibn Daqeeq al-‘Eid said: The trials (fitnah) of life are the things that the individual is faced with throughout his life of the temptations of worldly gain, desires and foolishness, the greatest of which – we seek refuge with Allah – has to do with how his life will end at the time of death. With regard to the trials of death, it may be that what is meant is fitnah at the time of death, and it is mentioned in conjunction with it because it occurs close to it. Based on that, it may be that what is meant by the trials of life is what comes before that. Or it may be that what is meant by it is the fitnah of the grave. End quote.
Secondly:
The fitnah of the Shaytaan that affects the Muslim at the time of death may be in the form of waswasah (whispers, insinuating thoughts), just as occurs throughout his life.
But is there any proof in the Sunnah to indicate that the Shaytaan appears in the form of the most beloved of people to the one who is dying, and calls him to Christianity or Judaism?
That was mentioned by some of the scholars in their books:
Al-Qurtubi said in at-Tadhkirah fi Ahwaal al-Mawtaa wa Umoor al-Aakhirah (29-30):
It was narrated from the Prophet (blessings and peace of Allah be upon him) that when a person is dying, two devils sit with him – one on his left and one on his right. The one who is on his right appears in the form of his father and says to him: O my son, I care for you and love you; die following the religion of the Christians, for it is the best of religions. The one who is on his left appears in the form of his mother, and says to him: O my son, my womb was a vessel for you, my breast gave you to drink and my lap was a pillow for you; die following the religion of the Jews, for it is the best of religions.
This was also mentioned by Abu’l-Hasan al-Qaabisi in Sharh Risaalah Ibn Abi Zayd, and something similar was mentioned by Abu Haamid in Kashf ‘Uloom al-Aakhirah
But what al-Qurtubi mentioned was not based on any evidence from the Qur’an or Sunnah, because there is no proven hadith to that effect. Rather this is one of the reports that some scholars transmitted in their books, but they are not to be found in the reliable books of hadith.
Al-Albaani said in as-Silsilah ad-Da‘eefah wa’l-Mawdoo‘ah (3/645):
As-Suyooti said: I have not come across this in any hadith. End quote.
So it is not permissible to attribute that to Islamic teaching, and it is not permissible to scare people with it, or to say that the Shaytaan will inevitably appear to everyone at the time of death. Islamic teaching does not say that; rather what is proven is that the Shaytaan whispers (waswasah) to the dying person and tries to tempt him to be discontent with the divine decree because of the intensity of the distress he is going through.
Even though that is not proven in any hadith, we cannot deny it either, because the Shaytaan has many tricks and ways, and he is able to appear to a person in various forms, so that may happen to some people at the time of death.
Shaykh al-Islam Ibn Taymiyah was asked (Majmoo‘ al-Fataawa, 4/255) about the presentation of different religions at the time of death: is there any basis for that in the Qur’an and Sunnah, or not?
He replied:
, Lord of the Worlds.
With regard to the presentation of religions to a person at the time of death, that is not something that happens to everyone, nor is it ruled out in the case of everyone. Rather religions may be presented to some people before they die, and in the case of others that does not happen. It has indeed happened to some people. All of this comes under the heading of the fitnah or trials of life and death from which we are instructed to seek refuge with Allah in our prayers.… But at the time of death the Shaytaan is most eager to tempt the sons of Adam, because it is a time of great need. The Prophet (blessings and peace of Allah be upon him) said in the saheeh hadith: “Deeds are judged by the way they end.” And he said: “A person may do the deeds of the people of Paradise until there is nothing between him and it but a cubit, then the decree overtakes him and he does a deed of the people of Hell and enters it. And a person may do the deeds of the people of Hell until there is nothing between him and it but a cubit, then the decree overtakes him and he does a deed of the people of Paradise, and enters it.”
Narrated by al-Bukhaari (3208) and Muslim (2643).
Hence it was narrated that the Shaytaan is never more eager to harm the son of Adam than at the time of his death. He says to his helpers: Go after this one, for if he gets away from you, you will never catch him. 
The narration of ‘Abdullah ibn Ahmad ibn Hanbal with his father – “Not yet” – is well known. End quote.
In fact that happened to some of the righteous.
Al-Qurtubi (may Allah have mercy on him) said in at-Tadhkirah (30):
‘Abdullah ibn Ahmad ibn Hanbal said: When my father Ahmad was dying, I had a piece of cloth in my hand to wrap around his jaw, and he would sweat and then come to and gesture with his hand: Not yet, not yet. He did this several times. I said to him: O my father, what are you saying? He said: The Shaytaan is standing beside me, biting on his fingertips and saying: O Ahmad, you got away from me, and I am saying: Not yet, not until I die.
I say: And I heard our Shaykh, Imam Abu’l-‘Abbaas Ahmad ibn ‘Umar al-Qurtubi in Alexandria, saying: When the brother of our shaykh, Abu Ja‘far Ahmad ibn Muhammad al-Qurtubi, was dying in Cordoba, it was said to him: Say Laa ilaaha ill-Allah, and he said: No, no. When he came to, we mentioned that to him, and he said: Two devils came to me, from my right and from my left. One of them said: Die as a Jew, for it is the best of religions; and the other one said: Die as a Christian, for it is the best of religions. And I was saying to them: No, no. End quote.
Thirdly:
If the Muslim understood the seriousness of the fitnah or trials of death, when one is dying, and that there is bound to be great distress, he would prepare himself for that day, equip himself with righteous deeds, and hope that Allah will decree a good end for him. For Allah, may He be exalted, will protect His believing slave, and if He sees that he is sincere at heart in his love for Him, He will protect him from error and will ward off temptation from him. So no one should think negatively of Allah, may He be exalted, for He is just and kind; He will not forsake His believing slaves, for He has forbidden injustice to Himself. So you will not find – in sha Allah – anyone whom the Shaytaan tempts in such situations except one who turned away from Allah and turned towards the Shaytaan. Such is the one over whom the Shaytaan gains control and confuses at the time of death, as he gains control over him and confuses him during his life.
Ibn al-Qayyim said in al-Jawaab al-Kaafi (62):
How can one be helped to a good end whose heart failed to remember Allah, who followed his whims and desires and was negligent? It is highly unlikely that the one whose heart is far away from Allah, may He be exalted, who is heedless of Him and who worships and follows his own whims and desires, whose tongue is devoid of remembrance of Allah, and his physical faculties are not involved in acts of obedience to Allah because they are distracted by disobedience and sin, will be helped to  have a good end. End quote.
And Allah knows best.