In Buddhism and dharmic faiths they tell you to not have attachments to things to achieve a state of freedom from the physical world. In the abrahamic faiths it’s not that, it’s about facing eternal punishment from the demiurge if you go against its desires. While obviously that’s not what happens, something else happens that some may not want. Attachments to the world in the forms of lust, gluttony, debauchery, or even to accomplishments or altruism can result in you becoming a ghost.
Becoming a ghost is not necessarily bad and you can be a very helpful spirit to the living people of the world and guide them to beneficial outcomes. You may need sustenance in some form such as food or coffee offerings, thoughts, or rituals performed to give you energy to interact with the physical world. You can also function like other spirits depending on your spiritual modus operandi. You may be able to sustain yourself off of sex and lust like some spirits, or bodily fluids. Some can sustain on dung and animal flesh while some of the less benevolent ones sustain themselves off negative energies such as violence, anger or suffering.
To avoid becoming a ghost, you must make peace with death and make the conscious choice to move on once you no longer have a physical body in the land of the living. This sounds simple, but many do not even realize they are dead which complicates the situation. Others may be so caught up on protecting their bloodlines or fulfilling desires from their lives they do not even visit hell. If you must become a ghost, just do not become a hungry ghost, causing problems for the living due to a lack of sustenance and feeding off of the living parasitically.