Let me warn you, there are no in-depth definitive answers. I've read as many books on the subject as I could possibly find, and I can tell you you will only find two kinds of explanations: science types telling you you're having mental problems, and spiritualist types telling you silly stories about spirits from another world. I have never found convincing arguments for either.
About six years ago I was resting on the couch and suddenly my deceased grandmother appeared in front of me. She was there for about a minute, she looked as real as any object in the room. She said nothing, only stayed there, then disappeared. It was the only time I had such an experience, never before, never after. When it happened, I also wanted to know about it. All I discovered is that people have always had those experiences, and it seldom amounts to anything other than something strange that happens to them but leaves no consequences, except perhaps in how they see the world.
I do not believe it was my grandmother's "spirit" I was seeing. And I didn't have to take any pills to prevent it from happening again, so it's clearly not something wrong with my brain. I came to think it's just a strange kind of dream, nothing else. I have certainly seen my grandmother several times in my dreams; why should this not be just another one? And I find it perfectly possible to have a dream about someone entering your body, and the dream look so real as to be frightening. But if you think about it, it can't possibly be anything other than a dream.
Now if it kept recurring, I would seriously fear for my sanity. Or my life.