A no-contest clause works on incentives rather than prohibition. It cannot stop anyone from filing suit. What it does is attach a price: a beneficiary who challenges the document and loses forfeits whatever they were left. The calculation it forces is simple — risk a certain inheritance for the chance of a larger one.
That calculation only works if the gift is meaningful. Leaving someone one dollar with a forfeiture clause attached deters nothing, because a person with nothing to lose has every reason to sue. Practitioners who use these clauses well pair them with a gift large enough that walking away from it hurts, which is a deliberate and slightly uncomfortable drafting exercise.
Enforceability varies by state and it varies a lot. Some states enforce these clauses fairly readily; others refuse to enforce them at all; many sit in between and decline to enforce where the challenger had probable cause to bring the claim. Because that probable-cause exception is common, a clause is rarely a defense against a genuinely well-founded challenge.
Note also what counts as a contest. Well-drafted clauses define the triggering conduct precisely, since a beneficiary asking a court to construe an ambiguous provision, or objecting to an executor's accounting, is doing something different from attacking the will's validity. A vague clause can chill legitimate questions or, more often, fail to catch the conduct it was aimed at.