Sometimes, a story just begs for a double-cross. Somebody hires the player characters to complete a job, everything goes fine, until right at the very end the PCs discover their employer was the baddie all along! Now their employer wants them dead, or refuses to pay, or intends to conquer the world with the device they've just delivered. It's a time-honoured and genuinely compelling trope.
The turnabout employer was a staple of film noir, and it's a common story element in Shadowrun, The D&D movie, at least from the trailers, incorporates it into its storyline. The problem is, this trope can be problematic in real life D&D games, and despite using it myself from time to time, it's something that ultimately most Dungeon Masters ought to avoid, and here's why.
There's an unspoken agreement between the Game Master and the players in an RPG. When the GM presents players with an adventure, they accept it. It's an important agreement, because unlike a board game or a card game, there's no universal goal to an RPG. In one game you're rescuing the princess, and in the other you're looting dungeons for gold, and in another you're robbing a corporation of top secret data files. The goal doesn't come written in the rulebook, it's delivered to you fresh with every game you play. For that to work, players have to accept an adventure proposed by the Game Master through an in-game NPC.
This is pure meta-gaming, by the way. In-game, there's often several reasons a player character might decline a quest. Maybe it's too dangerous, or maybe they just got back from saving the world an hour ago, or maybe the pay's not good enough, or whatever. When an NPC handing out quests betrays their trust, you give your players one new, in-world reason to refuse a story prompt. You train your players to say "no" to an adventure.
This is a problem when you only have 4 hours to play, but your players spend the first hour looking for a different quest from an apparently more trustworthy NPC.
If you've trained players to distrust NPCs, how do you get that trust back? What possible in-game ability would a player character have to tell a trustworthy one from one who's going to betray them in the final act? There's no way easy for them to establish trust after even just one NPC has spoken, for all practical purposes, in the Game Master's voice to lead them on a quest that has turned out to be a trap.
The tragedy is that your players haven't lost trust in an NPC. They've lost trust in you, the Game Master. Your players have recognized that you're trying to trick them into playing a specific kind of game, and because that type of game happens to end in betrayal, they naturally intend to avoid it.
A twist ending of betrayal doesn't usually feel good. Sure, sometimes there's a "I should have seen this coming!" moment, but more often it's either "I did see this coming, and I said so, and the Game Master ignored me and made me accept the job anyway" or "I should have seen this coming, but I didn't, and now I feel bad that the Game Master tricked me, and also why is my player character so stupid?"
I've played in games where it's painfully obvious to players that the NPC with a quest is going to betray them in the end. We, the players, have even said so to the Game Master. But the Game Master, sometimes with a knowing wink and other times with a very effective Poker face, denies everything. So we do the job against our better judgement, knowing that to refuse the job would mean ending the game before it's even begun. And in the end, of course, the NPC turns on the PCs.
It's not a twist ending when every player at the table has expected it from the start.
Worse still, all this trick has done is remove player agency. You've communicated to your players that their voice has no meaning in this game. They can declare that they don't like a situation, but if the Game Master decides that a story point is essential for the group to get to the end of the adventure, then that's what their characters are doing. Like it or not.
That's the opposite of what a Game Master wants from players. You want your players to think for themselves, to place themselves in their characters, to see the game world through the eyes of their character, to use the logic and gut instincts of their characters. In fact, we often say that players must ignore real-world knowledge to avoid meta-gaming. So why force players to use meta-game knowledge ("this adventure must happen, so we have to accept this adventure no matter what") to make the game happen?
There are exceptions, of course.
The culture of Shadowrun, both in-world and around the gaming table, encourages a kind of film noir fatalism. When you get called into a meeting with a client (invariably called only "Mr. Johnson"), you do your best to ensure you're meeting on neutral and safe ground, free from surveillance, with lots of contingency plans. You do this even though you haven't even been hired for the job yet. This is how you prepare for just the interview!
That's the culture of many Shadowrun games, and the tone set in the game source books. It's trivial to build that same culture at your table. A party full of rogues or rogue-likes probably should distrust their employers. A party in a particularly cut-throat setting could easily justify this kind of setup.
But the setup must be clear. There can be no question about it. NPCs with quests are, on the whole, deadbeats at best and back-stabbers at worse. Player characters accept the jobs anyway because they just can't find better work, or because they're evil themselves, or whatever. Whatever the reason, it has to be a known factor, as it is in Shadowrun.
You can tell the players, in no uncertain terms, of the NPC's intent. Just tell them, using Passive Perception with no roll required, that they get the feeling that this NPC has no intention of paying (or whatever.)
That seems like it would spoil the twist ending, but you might be surprised at how a twist is still satisfying even when players know about it. It's satisfying because players know about it, so they can plan for it. And that feels good to players. They conned the con artist, tricked the trickster, out rogue'd the rogue. It's even more satisfying than a twist they didn't want. Plus, there's no loss of trust, and no hurt feelings.
You can simulate a betrayal twist ending by having the NPC get betrayed along with the player characters. This, again, is borrowed slightly from Shadowrun tradition, where often the big mega corporation swoops in like an anti-deus-ex-machina to bring misery to both sides of the deal.
It works best when the betrayal makes sense. You probably don't want a random NPC who nobody's heard of. But if there's an NPC who the players are suspicious of, or particularly hate, then that NPC could be perfect for the role of party crasher in the final scene. Just as the trustworthy NPC is handing the player characters the bag of coins and XP, in comes the scoundrel character to arrest, fight, or otherwise put a stop to the transaction.
Everybody feels equally betrayed, and not by the Game Master.
The premise of D&D is often pretty weak, in the best of cases. Even a perfectly reasonable story hook often amounts to "find the least qualified (level 1) group of strangers, and send them (and ONLY them) on this vital quest to save the entire world." It's a convention of the hobby that player characters are offered jobs for which they're not qualified, by total strangers offering modest rewards for impossible tasks. It works because it enables us to all play the game. If you betray that convention, though, you can make it unpleasant to either play the game, or unpleasant to start a new adventure. So give it serious thought when you're considering having your quest-giving NPC betray your player characters.