There are many theories of what ghosts (if they indeed exist) are. Some people believe that ghosts are the residual energy left behind by an emotionally strong person or event. This theory holds that more energy/electrical impulses are expended during periods of high stress or excitement, and that the energy lingers for a long time.
Freud thought that ghosts are actually the visions of people who are afraid of death. In this sense, ghosts would not be real at all but rather a projection of our subconscious mind.
A somewhat plausible theory is that ghosts are telepathic images. That is, a sensitive person would pick up past vibrations from the area they were in and witness an event or person as it appeared many years ago. This would also explain instances where a person sees a loved one at or near the moment of the the loved one’s death, since the loved one could be unconsciously projecting their thoughts to the receptive person.
Ghosts might also be the result of time slips, if time is nonlinear. An event that happened in the past might be seen briefly in our time because of a fluctuation in time/space.
On his show -Mysterious World-, Arthur C. Clarke has speculated that our minds might play images to our eyes (the same way our eyes relay messages to our brain, but in reverse), almost like a movie screen. In this way ghosts would be bits of our imagination come to life.