Meaning of the hadeeth; “Then the divine decree overtakes him and he does the deeds of the people of Hell”
Question

I would like you to explain the meaning of this Hadeeth:
Narrated Abi Abdurrahman Abdullah bin Mas'ud, May Allah is pleased with him:
Allah's Apostle, the true and truly inspired said, "(The matter of the Creation of) a human being is put together in the womb of the mother in forty days, and then he becomes a clot of thick blood for a similar period, and then a piece of flesh for a similar period. Then Allah sends an angel who breathes the soul into him, and is ordered to write four things. He is ordered to write down his (i.e. the new creature's) deeds, his livelihood, his (date of) death, and whether he will be blessed or wretched (in religion). So, by Allah the One, a man amongst you may do good deeds till there is only a cubit between him and Paradise and then what has been written for him decides his behavior and he starts doing (evil) deeds characteristic of the people of the (Hell) Fire. And similarly, a man amongst you may do (evil) deeds till there is only a cubit between him and the (Hell) Fire, and then what has been written for him decides his behavior, and he starts doing deeds characteristic of the people of Paradise." Narrated by Al-Bukhari.
To be honest with you I feel bad sometimes that I may do good deeds while it is written that I am from the people of hell. I wish you answer me, thanks a lot for your efforts, may Allah greaten your reward.
Praise be to Allah.Ibn al-Qayyim (may Allaah have mercy on him) said:  
Those who are ignorant of Allaah and His names and attributes misinterpret their true meanings and turn people against Allaah and put obstacles between the people and loving Allaah and drawing close to Him without realizing. 
We will mention some examples of that, such as that they try to make people with shallow knowledge (or weak faith) think that no acts of obedience towards Allaah will do any good, even if that is done for a long time and a person has tried his utmost to do it properly both outwardly and inwardly; and that a person cannot be safe from the plot of Allaah, rather it is Allaah’s way to take the obedient one who fears Allaah out of the mosque and into the brothels, and from Tawheed and tasbeeh to shirk and flutes, and turn his heart from pure faith to kufr. They narrated sound reports concerning this that they do not understand properly, and false reports that the Prophet never said, and they claim that this is the essence of Tawheed. End quote. Al-Fawaa’id (159). 
Then he (may Allaah have mercy on him) said: … So this poor person no longer sees actions as being good or bad, so he does not feel comfortable when doing good, or feel distressed when doing bad. Is there any more effective way of putting people off Allaah and making them hate Him than this?! If all the heretics strive hard to make people hate religion and put them off Allaah, they could never have come up with anything worse than this.  
The one who has this belief thinks that he is reinforcing the ideas of Tawheed and the divine will and decree, and refuting the innovators and supporting Islam, but by Allaah, a wise enemy is less harmful than an ignorant friend. All the revealed Books and all the Messengers bear witness to the opposite of that, especially the Qur’aan. If those who seek to call others to Allaah follow the method that is established by Allaah and His Messenger in calling people to Islam, the whole world would be reformed in such a way that there would be no corruption after that.  
Allaah tells us – and He is the One Who is truthful and fulfils promises – that He only judges people on the basis of their actions, and recompenses them for their deeds, and the one who does good should not fear any injustice or unfairness; He will never cause the deeds of any doer of good to be lost, no matter how small, or wrong any soul in the slightest. “But if there is any good (done), He doubles it, and gives from Him a great reward” [al-Nisa’ 4:40]. Even if it is the size of a grain of mustard seed, He gives reward for it and does not cause it to be lost. He recompenses bad deeds with the like thereof, and cancels them out if a person repents, regrets it and seeks forgiveness and does good deeds, or if calamities befall him. But He rewards a good deed anything from ten fold to seven hundred fold or more. 
He is the One Who reforms the misguided and turns back to Him the hearts of those who turn away; He accepts the repentance of sinners, guides the misled, saves the doomed, teaches the ignorant, guides the confused, reminds the heedless, gives refuge to the lost. When He sends a punishment, it is only after severe rebellion and stubborn turning away, and after calling the people to turn back to Him. End quote. 
Al-Fawaa’id (161). 
There follows a great hadeeth which was narrated by al-Bukhaari (3208) and Muslim (2643) from Ibn Mas’ood (may Allaah be pleased with him). Some people were confused about the words of the Prophet (peace and blessings of Allaah be upon him) “one of you 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 one of you 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.” 
This is an example of what Ibn al-Qayyim (may Allaah have mercy on him) tried to point out of saheeh reports which are not understood properly. 
The answer to this is that it applies to the one who does not do deeds with sincerity and faith, rather he does deeds of the people of Paradise as it appears to people only, as is explained in another version of the hadeeth that was narrated by al-Bukhaari (4207) and Muslim (112) from Sahl who said: The Prophet (peace and blessings of Allaah be upon him) and the mushrikeen met in battle and fought. When the Messenger of Allaah (peace and blessings of Allaah be upon him) went back to his camp and the others went back to their camp, there was among the companions of the Messenger of Allaah (peace and blessings of Allaah be upon him)a man who killed anyone (of the enemy) who got in his way. They said: No one has done better today than So and so. The Messenger of Allaah (peace and blessings of Allaah be upon him) said, “Rather he is one of the people of Hell.” A man said: I am going to follow him. So he went out with him; every time that man stopped, he stopped with him, and when he hastened, he hastened with him. He said: The man was badly wounded, so he sought to hasten his death. He put the handle of his sword on the ground and its tip in the middle of his chest, then he leaned [on his sword] and killed himself. The man went to the Messenger of Allaah (peace and blessings of Allaah be upon him) and said: I bear witness that you are the Messenger of Allaah (S). He said: “Why is that?” He said: The man who you said was one of the people of Hell and the people were astounded by that. I said, I will find out about him for you. So I followed him until he was badly wounded, then he sought to hasten his death.  He put the handle of his sword on the ground and its tip in the middle of his chest, then he leaned on it and killed himself. The Messenger of Allaah (peace and blessings of Allaah be upon him) said: “A man may do the deeds of [the people of] Paradise, or so it seems to the people, although he is one of the people of Hell, and a man may do the deeds of [the people of] Hell, or so it seems to the people, although he is one of the people of Paradise.” 
As for the one who does the deeds of the people of Paradise in a real sense, sincerely and on the basis of faith, Allaah is too just, merciful and generous to let him down at the end of his life. 
Rather these are the ones who are guided, directed and helped to persist, as Allaah says (interpretation of the meaning): 
“Allaah will keep firm those who believe, with the word that stands firm in this world (i.e. they will keep on worshipping Allaah Alone and none else), and in the Hereafter. And Allaah will cause to go astray those who are Zaalimoon (polytheists and wrongdoers), and Allaah does what He wills”
[Ibraaheem 14:27]
“As for those who strive hard in Us (Our Cause), We will surely guide them to Our paths (i.e. Allaah’s religion — Islamic Monotheism). And verily, Allaah is with the Muhsinoon (good-doers).”
[al-‘Ankaboot 29:69]
“Verily, he who fears Allaah with obedience to Him (by abstaining from sins and evil deeds, and by performing righteous good deeds), and is patient, then surely, Allaah makes not the reward of the Muhsinoon (good‑doers) to be lost”
[Yoosuf 12:90] 
“They rejoice in a grace and a bounty from Allaah, and that Allaah will not waste the reward of the believers”
[Aal ‘Imraan 3:171]
Ibn al-Qayyim said in al-Fawaa’id (p. 163): With regard to the fact that a man 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, this refers to the actions of the people of Paradise as they appear to other people. If it was a righteous deed that is acceptable to Allaah, then Allaah would love it, accept it and not cancel it out. Someone may say that this phrase, “until there is nothing between him and it but a cubit”, does not support what we have suggested; the answer to that is that if deeds are to be judged by the way they end, this man did not have enough patience to complete his deeds properly; rather there was a hidden problem and something wrong with his deeds, by means of which he was let down towards the end of his life, so that hidden, serious, fatal problem caused his downfall at the time of need. If there was no problem and no insincerity, Allaah would not have turned his faith into disbelief. And Allaah knows better than all people what they do not know of each other. End quote. 
Ibn Rajab (may Allaah have mercy on him) said:  
The words “as it appears to people” indicate that what is hidden may be different from what is apparent to people, and that a bad end is because of a hidden problem in a person that people are not aware of, whether it is a bad deed and the like, so that hidden quality may lead to a bad end at death. 
Similarly, a man may do a deed of the people of Hell, but inwardly he has some good quality, and that prevails at the end of his life, and leads to a good end for him. 
‘Abd al-‘Azeez ibn Abi Rawaad said: A man was dying and he was being prompted to say Laa ilaaha ill-Allaah, but the last words he said were, “He does not believe in what you are saying” (referring to himself), and he died like that! 
He said: I asked about him, and found that he was addicted to alcohol! 
‘Abd al-‘Azeez used to say: Beware of sins for it is sins that led him to this bad end. 
In general, how people end up is the consequences of what went before, and all of that has been previously written in a Book, hence the salaf greatly feared a bad end, and some of them would feel anxious when remembering previous sins. 
It was said that the hearts of the righteous are concerned about how their life will end, and they always wonder: how will our end be? 
The hearts of those who are close to Allaah are concerned about previous sins, and say: How will our sins affect our ends? Sahl al-Tastari said: The one who has started his journey towards Allaah fears that he may fall into sin and the one who is close to Allaah fears that he may end fall into kufr (disbelief). 
Hence the Sahaabah and the righteous salaf who came after them feared that they might become hypocrites, and their fear and anxiety were intense. So the believer fears lesser hypocrisy for himself, and worries that it might prevail at the end of his life, and take him into a state of major hypocrisy, as it was mentioned above that hidden problems may lead to a bad end. End quote. 
Jaami’ al-‘Uloom wa’l-Hukam (1/57-58). 
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) said: The hadeeth of Ibn Mas’ood, “until there is nothing between him and it but a cubit” means, Paradise. It does not mean that his deeds bring him to this position until there remains nothing but a cubit, because if he were doing the deeds of the people of Paradise in a true sense, Allaah would not let him down because Allaah is more generous than His slaves. Imagine a person devoted to Allaah, and there is nothing but a cubit between him and Paradise: would Allaah turn him away?! This is impossible. Rather the meaning is that he does the deeds of the people of Paradise as it appears to others, until when there is nothing left of his lifespan but a little, his heart drifts away – Allaah forbid; we ask Allaah to keep us safe and sound. This is the meaning of the hadeeth of Ibn Mas’ood: when there is nothing left between him and Paradise but a cubit with regard to his lifespan, otherwise he was not doing the deeds of the people of Paradise in the first place. 
We ask Allaah not to misguide our hearts. (So the person referred to) is striving but in his heart there is some hidden evil that leads to his doom when there is only one cubit left when he dies. End quote from al-Liqa’ al-Shahri (13/14). 
Some scholars suggested that the person mentioned in the hadeeth may be striving in a true sense, until when his lifespan nears its end, he meets a bad end, and dies in a state of kufr or sin, but this is rare. This also goes back to something hidden or some serious evil quality that the person persisted in, such as corrupt belief or a major sin that dooms one to Hell, which led to his bad end. We ask Allaah to keep us safe and sound. So the hadeeth is to be understood as a warning that no one should rely too much on his deeds and we should ask Allaah for steadfastness until death, for hearts are between two fingers of the Most Merciful and He turns them as He wills. 
Al-Nawawi (may Allaah have mercy on him) said in Sharh Muslim: What is meant by a cubit is an indication of how close he is to death, and whether he will enter Paradise or Hell after that, and that there is nothing between him and his reaching that abode except a cubit, like that which remains between him and his reaching any spot on earth. What this hadeeth means is that this may happen in rare cases to some people, not that it is the norm. Moreover it is a sign of Allaah’s kindness and mercy that people may revert from evil to good in many cases. As for their reverting from good to evil, this is very rare and hardly ever happens. This is like the words of Allaah [in the hadeeth qudsi]: “My mercy precedes My wrath and prevails over My wrath.” That includes those who turn to the deeds of Hell by disbelieving or sinning, but they (the scholars) differ with regard to whether they will abide therein forever or not. The kaafir will abide in Hell forever, but the sinner who dies believing in Tawheed will not abide therein forever, as stated above. In this hadeeth there is a clear confirmation of the divine decree, and that repentance erases all sins that came before it, and that whatever state a person dies in, he will be judged accordingly, whether it is good or bad, but those who commit sins other than kufr are subject to the will of Allaah. End quote. 
What should be noted in this context is that the same hadeeth which confused you also contains the answer to this confusion. That is because it does not only confirm the concept of the divine decree and Allaah’s prior knowledge of His creation and His prior writing of their deeds, but in this and similar texts, in addition to all that, there is also affirmation of His commands and prohibitions, and that Allaah does not punish them or bless them on the basis of His knowledge concerning them only, but rather on the basis of what they did and earned themselves. 
Shaykh al-Islam Ibn Taymiyah (may Allaah have mercy on him) said: 
This hadeeth and similar reports point to two concepts: 
1 – The prior decree, which means that Allaah already knows who are the people of Paradise and who are the people of Hell before they do any deeds. This is true and must be believed in. Indeed the imams, such as Maalik, al-Shaafa’i and Ahmad, have stated that the one who denies this is a kaafir. Rather we must believe that Allaah knows everything that is going to happen before it happens, and we must believe in what He has told us that He has decreed will happen and has told us of it before it happens. 
2 – That Allaah knows all things the way they are, and He has made for things means that may lead to these things. So He knows that these things could result from these means, as He knows that this man may have a child by means of having intercourse with a woman that makes her pregnant. If this man says: Allaah knows that I am going to have a child so there is no need for me to have intercourse, then he is a fool, because Allaah knows that it will come about by means of what He has decreed of intercourse. Similarly a man knows that crops will grow as the result of his watering the seeds that he has planted, but if he says: He knows that it will happen so there is no need to plant seeds, then he is ignorant and misguided because Allaah knows what will come about by those means. 
Similarly, if He knows that this one will be blessed in the Hereafter and that one will be doomed, we say: it is because that one does the deeds of the doomed, so Allaah knows that he will be doomed because of his deeds. If it is said that he is doomed even if he does not do anything, this is false, because Allaah does not admit anyone to Hell except for his sin, as Allaah says (interpretation of the meaning): “That I will fill Hell with you [Iblees (Satan)] and those of them (mankind) that follow you, together” [Saad 38:85]. So He swore to fill it with Iblees and his followers, and those who follow Iblees have disobeyed Allaah, may He be exalted, but Allaah does not punish anyone for what He knows he will do until he does it. 
Similarly Allaah has decreed Paradise for the people who believe in Him and obey Him, so the one who is decreed to be one of them is guided by Allaah to believe and obey. The one who says, I will enter Paradise whether I am a believer or a disbeliever, if He knows that I am one of its people, is fabricating a lie against Allaah. Allaah knows that he will be admitted to it by faith, so if he does not have faith he will not be the one who Allaah knew would enter Paradise, rather he will be not a believer but a kaafir, and Allaah knows that he will be one of the people of Hell, not one of the people of Paradise. 
Hence Allaah commanded people to pray, seek forgiveness from Allaah and other means. The one who says, I will not pray or ask, and I will rely on the divine decree, is also mistaken, because Allaah has made praying and asking means of attaining His forgiveness, mercy, guidance, support and provision. If He has decreed some good for a person to be attained by means of du’aa’ that will not be attained it without du’aa’. What Allaah has decreed and knows about people’s situations and how their lives will end, it is only decreed through the means that lead to that specific outcome. Nothing in this world or in the Hereafter happens except through means, and Allaah is the Creator of the means and the ends. 
With regard to this matter, two types of people go astray. There are those who believe in the divine decree and think that it is sufficient to attain one's aims, so they turn away from the prescribed means and righteous deeds. They will end up disbelieving in the Books of Allaah, His Messengers and His religion. 
(The other group) ask for reward from Allaah as a hired man asks for his wages from his employer, relying on their own power, strength and deeds. These people are ignorant and misguided, because Allaah has not enjoined His commands upon people out of need, and He has not imposed His prohibitions out of stinginess, rather He has enjoined what is in their best interests and He has forbidden that which is bad for them. As He says: “You will never be in a position where you are able to harm Me and you will never be in a position to benefit Me.”  
The one who turns away from the commands and prohibitions, promises and warnings, and focuses only on the divine decree has gone astray, and the one who strives to follow the commands and prohibitions and ignores the divine decree has gone astray. Rather the believer is as Allaah says: “You (Alone) we worship, and You (Alone) we ask for help (for each and everything)” [al-Faatihah 1:5], so we worship Him by following the command, and we seek His help on the basis our belief in the divine decree. End quote. 
Muqtatafaat min Majmoo’ al-Fataawa (8/66 ff). 
And Allaah knows best.