Ultimately, the question of what can and cannot gain class levels is up to the DM. There's no real rule.
The Advancement portion of the creature's stat block is not really meant to answer the question, "Can this critter earn XP, and what happens if it does?" It is intended to give the DM a guide on how to make a tougher version of the critter as a greater challenge for the PCs. Many people use it for the unitended purpose, and would say that a ghost can gain levels if the creature it was in life could gain levels.
Personally, I'd usually not allow it. One of the major things about many undead, and ghosts especially, is that they are stuck in an immortal, never-changing existance. Is that consistent with changing, learning, and growing?