This is my theory:
When pokemon die, they go into another dimension. This dimension is essentially the equivalent of heaven/hell/some other more neutral location. If say, they were brutally murdered, or killed in some exceptionally violent and/or sudden way, their spirit may cross back over into the normal dimension, so that they may try to either find some sort of peace, through revenge, knowledge, or whatever allows them to accept their death, or, become a ghost pokemon, and try to live out the rest of the life that was taken from them.
For example, the Marowak in the pokemon tower that was killed by Team Rocket. I'm assuming it needed to be able to have a fair fight for its life. It never had a chance against team rocket, but being able to fight against someone and being defeated, it is able to find some peace. I'm sure it also likes the fact that you're avenging its death by defeating Team Rocket.
If, say, a young pokemon dies of an illness or something, before being able to have a long, fulfilling life, it might come back as a ghost pokemon, in the hopes of 'living'. Something like reincarnation. I imagine something like this happened with Ash's Haunter in the anime. It just wanted some fun and someone to share a few laughs with.
If they just die of old age, after having a happy life, they just go to the other dimension/heaven/etc., which I would assume is something that the pokemon finds pleasant.
But who knows. I for sure don't think all pokemon come back as ghost pokemon, but I suppose something like that is possible, and ghost pokemon are just what we see whenever they cross between dimensions at areas where the gap between the dimensions is bridged. Haunted houses, the Pokemon Tower, Mt. Pyre, etc.