Most deceased people do not become "ghosts" after death in the first place, as they move on relatively quickly after death to either a next life or to various places on the astral away from here.
People who's souls linger are often those who died tragic deaths, or who feel unsatisfactory over things in their life by their time of death. Lingering attachment and being unable to let go keeps them stuck here as they try to remain to handle any open ends left at the end of their life.
This happens as people do not realize one should do this while one is alive, or it happens when there is sudden and unexpected death. Especially if the person in question died too suddenly to be aware they did in fact die, or weren't conscious enough at the time of death to understand what had happened.
Confusion follows, as they exist in a strange state they do not know anything about. Out of habit, these people who have passed away, would try to follow their old patterns or return to places they know and look for people they know.
Some do this to try and find answers, or to tell people they know that they are still there and still exist, unaware of the fact they are no longer incarnate in the world.
Most people do not even believe an actual after life exists, or have the faintest clue what the after life is like. While many have warped ideas about it due to the abrahamic religions which they believe they will go to be "one with ugly jew in the sky" after death or something. None of this bodes well, as it opens one up to severe attack after death, or the confusion regarding what has happened to them leaves them vulnerable on the astral.
Souls who become "ghosts" are in essence not much different from who they were when they were alive. They have the same personalities, the same habits, even the same appearance in most cases.
Though this often deteriorates quickly after death, due to the soul not having an anchor to center itself, thus continuously dissipating over time.
The personality is affected by this, and by their experiences on the after life.
For example, one ghost may desperately try to find his old friends and communicate with them, then be highly disturbed to see them mourning his passing while he is still very much right there. The deceased person does not recognize how their state is different from the living, as they cannot distinguish between their present self and the people in the living world, due to the conscious mind being ignorant of the after life.
They see themselves being there, and thus they believe they are alive. Which, they aren't necessarily wrong about, as indeed they do still exist, and thus they would even take offense that others believe them dead when in reality they exist and aren't truly dead.
They fail to see how their existence is different from the incarnate people due to ignorance and disbelief at their situation for which nothing they learned while alive prepares them for.
In desperation they may try to communicate with their loved ones, yet to their shock nobody seems to hear them and they all act as if they aren't there anymore. This can make them more obsessive and even more determined to stay around until finally someone acknowledges them, which can turn into a haunting.
These things don't always happen, sometimes the person who has died will realize during those experiences that something is amiss and will put things together to understand they have passed away.
They may then change their approach in communicating with loved ones, or they may give up communicating and try to understand what to do next. The Gods to preside over reincarnation and they would pick people like this up or guide them to a next life.
People may also go unconscious as they pass. Instinctively they shut down and go through the motions without thinking or acting on anything, these people would attach instinctively to a new life, guided purely by the natural process of reincarnation, never experiencing any of it as they are wholly unconscious during the process, sort of like falling asleep.
For ghosts who stick around and continue "living" their life as they did before death, this happens when they are completely unaware they had died at all, and they lack the consciousness to even address the situation or even try to communicate with people, or perhaps they tried to communicate to no avail and ignore the strangeness in that to just continue living as they know.
They go on auto pilot and try to continue doing what they used to do, following habit and routine.
Sometimes they get stuck in this and end up doing this till they dissipate completely, almost as if they are stuck in a limbo. These ghosts will always appear at the same time, at the same places, never deviating from this. The longer it goes on, the less personality remains until eventually it is only a lingering figment of the being.
They can be snapped out of their trance if someone adept at working with spirits communicates with them. Doing so feels like waking someone up from a deep slumber. As if they had been sleep walking all this time.
In conclusion, the reason this happens is due to the ignorance on the after life in people and ignorance od the nature of their existence as multi layered being with a soul, as well as the lack of consciousness many people have in general.
How many people do you see going on full autopilot through life?
If living people already exist in such a non-conscious state, what about them when they pass away?
Though, there are autonomous means in place which lead deceased people to reincarnate, just like how the autonomous nervous system keeps your body functioning automatically even if one is in a coma.
This is why there aren't billions of ghosts haunting every nook and cranny of the Earth at this time, but only a small number of deceased people truly become ghosts who remain stuck for extended periods of time, usually due to their will interfering with the process of reincarnation and being unable to let go.
Unfortunately I do not know exactly how this autonomous means of reincarnation works, or why/how exactly it can get disrupted sometimes, so I cannot fully explain that.
Hail Satan!