You've run into one of the dangers of pre-planning a plot. I'll give some ideas at the end about how to plan campaigns so this doesn't happen as much in the future, but first we have to deal with the current situation. Other answers have dealt nicely with the "stay on the rails" and "take a short detour" options, so I'd like to talk about a third choice:
Take a new path through the bush. Forget the plot you had lined up. You've got interesting people and conflicts already present in the world and waiting in the wings, but you can let go of exactly how you expect it to play out. Go along with the PCs' choices and look for opportunities to introduce the interesting people and ideas you have prepared. Instead of killing off this NPC and negating the PCs' hard work, use the NPC as a gateway to new adventures which will incorporate your ideas in new, interesting ways.
It shouldn't be too hard for, e.g., someone connected with the NPC to kick the bucket in a ghost-inducing way, so the NPC drags the party in for the ride and you get a ghost investigation on the party's terms rather than on your own. Players tend to be more engaged with plots that arise from PC agency than plots which are thrust upon them.
Now lets talk briefly about avoiding this kind of situation in the future. For me, the key lies not in how I plan, but in what I plan. Instead of creating interesting stories to walk my players through, I need to create interesting people, situations, and conflicts which are happening when the game starts.
My favourite kind of game prep is to set up a complex set of NPC/faction/world interactions and then watch my players roll through them like a lopsided bowling ball. This way, my players can engage with a world and have their choices matter because they're interacting with dynamic processes. As the party acts and makes choices, I'm free to have the world react: NPCs change their plans based on PC action, natural events occur when it's most dramatic, and so forth.
An RPG story is about the PCs, so I like to give them a chance to really make the world sit up and notice their choices. The best way I've found to do this is to avoid planning stories that hinge on the players making certain choices.
Be an NPC, not the GM
The GM, as an entity, must be infallible to a certain degree, because he is the PC's conduit to the game world. If he says the party meets an NPC, the party is now reacting to this NPC. If he says here there be dragons, the party stocks up on burn ointment. Players are likely to feel betrayed if this turns out to be false - not false in a "the princess we were sent to save is the dragon we need to save her from" but false in a "my combat guy has no skills relevant to a campaign centered around gangster rap battles" way, or an "I took an evening of my time to come roleplay a courtly flower arranger, not punch out Cthulhu as a superhero" way.
Enter the unreliable narrator. The GM needs to be infallible, but the NPCs need not be, so you can avoid the backlash by blaming it all on a made-up guy. If you are exposition-dumping, feel free to use crazy old man McGuckett as your mouthpiece. If he says he saw cultists summoning demons in the swamp, that's what he thought happened, but it's not the GM's fault the PCs took his word for it. Or he was lying and the PCs are now about to be ground into mincemeat at McGuckett's terrifying shed of rusty chainsaws and leather masks.
This works great when you can have an NPC travel with the party, and be their eyes and ears in terms of a certain task. A party of samurai may be escorted by a local shaman who claims to talk to the spirits, but in actuality is just making things up as he goes along. In an urban adventure, a lawyer, accountant, or other suit-type can string the PCs along in a nefarious scheme, or a scientist can ask them to do progressively stranger things...
Player vs character knowledge
Another issue is that your player's first session behind the sheet of Winnie the Wizard isn't Winnie's first day alive. She's lived in her world for decades, and it's not wrong for her player to expect that the GM will tell her things that Winnie knows but the player does not.
This one is actually very simple to fix - engineer a travel episode that sees the PCs arriving in an unfamiliar land. This will also help by gradually introducing the genre shift and making the PCs feel like it wasn't sprung on them.
One reflex might be to bar all kind of character knowledge, but you can actually use it to your advantage if you do it right. The perspective of a stranger in a strange land helps reinforce things that the players might not find weird, but their PCs sure as hell should. For example, your players might not know what color magic is supposed to be, but as soon as Winnie casts her purple spell and it comes out green she knows that something is afoot.
Best Answer
Dungeons and Dragons is all about PCs doing crazy things and the DM reacting to them. Granted, groups are as diverse and varied as fish in the sea, but ingenuity is a universal trait across games. You see this in character optimization, wacky solutions to problems (like the kitten Sleep-spell defense), and in any DM who is writing up his next encounter.
As a general response to your problem, the D&D rules are a framework from which to build your world and encounters. Take out the orcs, the dragons, etc. and you're left with a system constructed and refined over the years to simulate scenarios in a (mildly) balanced way. Ever come up with an insane hypothetical question and wonder how to act it out? I like to think that this is how Gygax and Co. first came up with the idea for D&D.
To return to your question, remember that the rules are there as a guide to these situations. They may not cover all things, but they encourage improvisation. For example, advantage/disadvantage is a useful tool in approximating some situations. Let's take a look at your situation in particular:
This is a workable scenario. It sounds reasonable and rewards the PCs for their creativity. However, there are plenty of other ways this could work out:
All of these are just ideas. The world is the DM's sandbox — don't be afraid to explore it. It's a game, and at the end of the day you and your players will figure out exactly how much they enjoy your work.