Just before someone dies, there is a phenomenon known as terminal lucidity, in that moments just before death, patients will have a moment of full mental clarity.
It is often at this time they may talking about deceased loved ones in the room that have come to take them or reach up and smile to an invisible person as they take their last breaths.