There are countless reasons for people to see ghosts, so it's not just one easy-to-explain answer, IMO.
Some people are brought up to believe in such things, especially older generations, in certain parts of the world. If you've ever been to Romania, they're very superstitious about all kinds of things, and it's the same in a lot of places throughout Europe, Germany has many superstitious people, much like parts of Ireland still do. I mean, we're talking people who still genuinely believe in werewolves, banshees, fairies, etc.
Many people simply want to believe in such things, and thus, they do.
Other people may just be influenced by stories they've heard, or things that frighten them.
People who go into old buildings, and have all of these wild stories floating around in their noggins are obviously going to think that every sound they hear or every shadow they see is likely a spirit.
Places where murders, executions, suffering, etc, have taken place, are all going to be regarded as being places where ghosts may exist.
It's a very interesting subject that I often like a bit of banter about, and like I say, it's not just one simple explanation, it's just a storied and weird world of legend, myth, religion, fear, and all manner of things which contribute to people believing in ghosts, and any number of strange things, tbh.