I’m often conflicted about letting the “good guys” (the players) always win. First off, it seems odd. I mean nobody ever wins all the time. There should be times when they suffer setbacks. But these are the players, and truth be told, people don’t want to spend their entertainment time losing. So can you make it challenging enough to make it thrilling, but still allow them to win all the time? I don’t think so.
But then the problem is pride. If faced with a situation that they truly cannot win (at least with the strategy they are using), will they retreat? Will they blunder forward killing characters off, or will they move away to fight another day? Depends on the players, but there are a lot of them out there who will not bother to break off, no matter how bad it is. These folks are usually pretty pissed when their characters die too, even though most GMs would perceive this to be the players fault.
You want the keys to the kingdom? You want the best GM advice ever? Here it is: There are more of them than there are of you. What’s that mean? It means if you place them in front of an unsolvable puzzle - at least one that you don’t already know the way out of, then they are still likely to come up with a solution. This works! Challenge them, but not in a life threatening way. What happens if they fail? Well, in some cases, they may need to go get more guys - hire some NPCs that have talents that they don’t have. In a lot of situations, they will come up with some crazy way to use the spells or abilities that they already have in ways you never would have thought of and might be able to win through that way. (You can often be a little more lenient here, especially if it adds to the story line.)
If you know how to beat it, then the players who know you are very likely to figure it out. After all, they know you, and probably know how you game master. If it is more of a challenge, and not simply an unwinnable fight, then failure (should it come) is not lethal, so they lose, but their characters are still alive. Of course the challenge can easily be - How do we get past an army 1,000x bigger than out party? Let’s hope they don’t start by rolling for initiative.
There’s a cop out here too. If you know how to beat the trap/puzzle/challenge, but they don’t figure it out, then after the game, some players are going to see this as you taunting them. “You couldn’t figure it out! All you had to do was ...” OK, for some of you GMs, they might be right and you were taunting them. But if they get all frustrated and ask what they were supposed to do, and you say, “I don’t know. I thought you’d figure a way out that would make it work, but I didn’t have an easy solution.” How do they argue with that? In fact, you’re complimenting them and saying that they (at least collectively) are smarter than you are.
Into each life a little rain must fall, even PCs. Make them work a little harder for it, even if that means they fail. Unfortunately, this might mean that you have other stuff planned for them. If they give up on one mission because you made it a little too hard, they aren’t going to be too happy about sitting around staring at you. Oh, and don’t gloat! It’s bad for their egos!