yam yam vaapi smaranbhaavam tyajatyante kalevaram | tam tamevaiti kaunteya sadaa tadbhaavabhaavitah || Bhagwad Gita, Chapter 8, Verse 6 ||
Lord Krishna explains to Arjun, whatever is the thought of the being when he leaves the body at the time of death, that and that alone one attains, being absorbed in its thought forever.
The body here does not refer to what you see in the mirror but extends beyond to include the five sheaths – Annamaya, Pranamaya, Manomaya, Vigyanmaya and Anandmaya. An ordinary being desires only the physical and exists at the level of the Annamaya (physical body) which in turn is controlled by the Pranamaya kosha comprising of prana. The soul stays in the body till the time there is prana in it. What we call as death is a state when there is no prana left in the body and hence the soul leaves the body.
The soul might leave the body, but it is still tied to its desires and karmas. You must have heard of the 1943 famine of Bengal. It saw 3 million die for the want of food. In those times, people had reported mysterious rattling of vessels in the kitchen. What made the vessels rattle? It was the hungry spirits of those who passed away, their desire for food was so strong that as soon as they left the body, they took shelter in the vessels where food was kept.
Alexander the Great had asked his General to leave his empty hands hang out of the coffin upon his death to show the world that you enter and exit empty-handed. You will never find vedic rishis saying so…they knew that you take with you your desires and karmas and they decide your next birth and body.
So if all your life you have thought of just yourself and done nothing for anyone else, that is, then that will be your thought at the time of exiting the body as well, and the next body will be in as per that, maybe the body of an animal, for that is the nature of an animal. Nature of humans is to protect and provide for those weaker than them, and the one who fulfils this is entitled to higher births and subtler bodies, says Gita.