What is a ghost, exactly? Do they even exist? Or are they figments of the imagination? Good questions! I can answer only from my personal experience over the past 30+ years. Someone who has had different experiences than I have, would undoubtedly have different answers and beliefs regarding the spirit realm.
To understand how "ghosts" are created, let's first examine the death process. Most of us identify more with our physical bodies than with our spirits. The "you" that walks around everyday, the "you" that goes to work and comes home, the "you" that runs errands and plays tennis, is not the real "YOU". Your body is simply a shell. You are attached to it now because you are "in" it. Once your spirit leaves your physical body, you see that it was just a vehicle you used to get around while you were here on earth. It holds no more importance to you than an older car that you are ready to trade in for a newer model. You may have some emotional fondness for it, but that's it.
The real "YOU" is the part of you that thinks, that loves, laughs, feels hope and joy, and has faith that there is a God. This "you" is your soul, your essence, the part of you that observes the world while living in it. We are all spiritual beings FIRST, physical beings second. When your spirit leaves your body, your consciousness does not "die", nor does your personality change. You are still "YOU" - but better! You may become aware that you have no pain. You feel light and buoyant. This is because the spirit body cannot be damaged. Whatever ills your physical body suffered, your spirit body will not share them. You will be whole and beautiful and filled with light. (do some reading on Near Death Experiences, they give a great idea of what the death experience is like for the many thousands of people who have experienced it)
Okay, so there you are, standing outside of your body, wondering what just happened. At this point a Spiritual Guide may appear to help you to the other side, or perhaps you see a speck of Light in the distance, and begin to move toward it naturally. This is the defining moment where "ghosts" are created. What if the person that just died did some really terrible things in their lifetime, and they just know that God is going to send them to Hell forever. How likely are they to want to see God? Not very! So they move away from the Light. Or maybe they don't know what that Light is, so they are naturally afraid of it. This is common for Athiests, because they expect their consciousness to just "blink out" when they die. When that doesn't happen, they can become confused and may not realize that they can move on. Sometimes, if a person dies in a very sudden, unexpected way, they might have no idea that they are "dead." They find themselves standing at the scene of the accident, wondering what happened.
Sometimes people do realize they have died, but they don't want to move into the Light right away, because they want to be assured that their loved ones will be okay without them. They may stay around for a few days, even attending their own funeral, and try to comfort their family. This happens a lot more than you might think! However, these types of spirits often move on shortly afterwards, and do not become ghosts.