Assuming an ideal opamp, an ideal audio driver in the headphone, an ideal mic that is the same distance from your ear canal as the driver, and you invert the audio from the mic, then sum it with the incoming audio with another ideal opamp, and put that audio in the ideal driver, you will have a perfect noise canceling device, if and only if the headphone is perfectly aurally transparent and does not passively attenuate external sounds before they reach your ear.
The truth is that every step I mentioned, every component in the circuit, electrical and aural, has a different frequency response from everything else, and there is no such thing as an ideal component, nor a perfectly aurally transparent headset.
So, in theory, if you invert the ambient noise, sum that inverted ambient noise with the incoming audio, the audio given to the driver will cancel out the ambient noise. In theory only.