On the authority of Aboo 'Abdur Rahmaan 'Abdullah ibn Mas'ood (radhiAllahu 'anhoo) who said: Allah's Apostle ('allyhee salat wassalam), 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 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). Then the soul is breathed into him. So, 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 until he enters it. 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 until he enters it."
(Al-Bukhari #3206, #3332, #6594, #7454; Muslim #2643; Aboo Dawood #4758; at-Tirmidhee #2137; Ibn Majah #76; Imaam Ahmad in al-Musnad #3624)