But since I deal in the Middle Ages, we'll just go there.
It was believed that, in the medieval period, ghosts would appear to loved ones because the ghosts were in Purgatory and pleaded for the prayers of the living to shorten their stay there. But were they the genuine article?
It seemed that only Christians had authentic ghosts. Jews and Muslims didn't have real ghosts, only demonic spirits pretending to be their dearly departed.
Evil spirits claimed to be the Christian dead, so all had to be tested, and could be found wanting. In 1458 one seemingly benign ghost was unable to recite a prayer when tested, thereby revealing itself as a diabolic spirit.
In fact, many of the ghost stories are distinct in their religious overtones--as would be expected of a people so entrenched in religion in their everyday lives. These ghostly visitations always seemed to involve the ghost tell the visitee of the horrors of Purgatory--and the pits of Hell that they could just see beyond. They were warnings, to be sure, and these hauntings usually ended when the ghost was prayed for enough and left the bonds of Purgatory for his or her Great Reward. Ghost stories only changed to horror stories as religion didn't have the iron embrace over an entire population as it did in the past. It seems when spiritualism took hold of the Victorian imagination, speaking to one's dead relatives and the horror of hauntings took on a completely different tone. You can read a little about that on my post about Ouija Boards.