There are four pieces of advice I give other GMs when I hear them complaining about stuff like this.
You should not actively circumvent player choices.
Spell slots in 5e are at a premium, as are known spells for most classes. In other words, unless you’re a Cleric or a Wizard, picking a given spell means you are actively giving up other spells, which affects the power balance of your character.
Comprehend Languages, in particular, is a choice that makes you give up combat versatility and/or guaranteed useful social RP benefits in exchange for a very binary (it’s either nearly essential or almost completely useless) exploration focused benefit. Even the other breaking spells though have such trade-offs. Invisibility, flight, and teleportation being useful are super dependent on the campaign setting and the rest of the party, polymorphs are only powerful if you need a tank or in the unlikely case that you need a good disguise but your opponent is not able to check for magic, and Wish, while game breaking in some cases, is entirely dependent on what the GM allows (at least it doesn’t cost XP like it used to though).
When players pick up these spells, they are making an active choice because they think they will be useful. The point of the game is for everyone to have fun. How fun is it to pick up a spell that you think will be useful, giving up on other things in the process, only to have it be completely useless?
Given this, I not only don’t try to avoid direct one-shot solutions, I go out of my way to include them when I know the players will have a way to trivialize them (provided of course that they are not critical to the story). Not huge numbers of them of course, but at least enough that the players don’t end up feeling like they made poor choices.
You must think like the in-universe creators of the traps and puzzles when creating traps and puzzles.
Magic is the most blatant thing here, you need to remember that it exists, and design your traps and puzzles around that assumption.
However, you also need to apply some common sense. If some ancient civilization built a tomb, would they really leave behind detailed step-by-step instructions to get inside? Of course not! If any info exists about getting inside or circumventing the traps, it will be at best figurative, probably requiring an understanding of the ancient culture itself to understand, and at worst fragmentary to non-existent, all because when it was created, that language was commonplace, and therefore writing down proper instructions would have enabled anyone to get in.
You need to think along those same lines when designing puzzles and traps. The party might get lucky and stumble across a figurative back door left by the designer, but in most cases they won’t be so lucky, and will have to figure things out the way the designers intended, which will almost certainly not consist of just casting a specific spell (though if it was a meritocratic society run by mages, the ability to cast a specific spell might actually make sense as a key).
Important puzzles and traps should take multiple steps to solve and should also have multiple possible solutions.
As a really simple example, years ago I did a dungeon crawl campaign where the party eventually ended up in a room with only one door, but which they knew was not the end of that particular path. While I did not explicitly list the options, they had three ways to possibly proceed that I had planned for. They could backtrack and go around (they had been mapping the dungeon so far, and it was not difficult to see from the map that there was at least one other path to where they were going that did not involve this room), they could go about finding the secret door and then just force it open with brute strength, or they could decipher the ancient text on one of the walls and puzzle out what the poem (actually Lewis Caroll’s How Doth the Little Crocodile) indicated for them to do. Given the party composition, any of those was a valid solution, and the option of backtracking would have been valid no matter what the party composition was. In practice, they actually ended up accidentally triggering the third option (it required them to pour water into a set of small golden bowls shaped like crocodile scales, and the wizard accidentally flooded the room (thus indirectly filling those bowls) when they were fighting some enemies that showed up part way through the party debating about how to proceed).
Unless you are designing the campaign knowing ahead of time what the party composition is and having a good idea of how they are likely to play, all major story related puzzles and traps should be built like this, and should also ideally follow the Three Clue Rule (which my above example technically did not), but you should not actively prevent creative or accidental solutions.
The reasoning behind this is twofold:
- Requiring multiple steps makes it much less likely that the players will trivialize the puzzle with a single spell. It also makes it feel more significant, because it requires a larger time investment to solve it.
- Providing multiple possible solutions helps ensure that the party will actually be able to solve it, independent of how they built their characters and independent of how they approach the problem. It also makes it more likely they will be able to solve it without having to come back to it, which helps keep the story moving forwards.
Conversely, unimportant and optional puzzles and traps should be simple, and it should be possible to trivialize them.
Put simply, if something does not matter directly to the story and only exists to provide some loot, then it should usually take as little time within a session as realistically possible. Allowing players to trivialize stuff like this by simply being appropriately prepared is a good thing, because it provides validation for their choices and it minimizes the time that gets taken away from actually propelling the story forwards.
This is essentially a non-binary application of the dramatic principle of Chekhov’s Gun. If something is completely irrelevant to the story (and not important for setting the mood), then it should not be included at all, if something is critical to the story, it should absolutely be included and have significant time devoted to it, and things that are only somewhat relevant to the story should only receive as much time as is needed to account for their relevance.
Would you, as the GM, rather have half the session spent with the party trying to figure out how to get a +1 longsword for the fighter because the puzzle is to complicated for them to figure out, or instead have them spend that time working on moving the story forwards? Unless you are a particularly adversarial GM with a (very bad) players vs GM mentality, you almost certainly would rather see the story moving forwards. The same is generally true of players as well, they tend to prefer to actually keep the story moving instead of sitting around figuring out a puzzle to get a possibly trivial item. The difference here is that the GM knows they are wasting time, while the players may not.
Avoiding situations like that is important to help keep the players happy and engaged, and one of the easiest ways to avoid such situations is to make the complexity of puzzles and traps directly correlate with their relevance to the story.