You do it with the waves inverted otherwise the delay would have to be dynamic and frequency dependent.
Normally sounds don't change in frequency that often so that's good enough (tm). I can hear myself typing now (short burst of sound), but the washing machine nearby, which is louder without earphones, is completely gone.