Basically, the missiles are engaged in a (deadly) game with the aircraft every time a missile is fired. All countermeasures are based on 'confusing' the missile homing systems to a certain extent, usually in the terminal stage. Missiles (or AD systems in general), use a number of methods to overcome this.
The general term for overcoming the countermeasures used by the aircraft is called counter-counter measure (CCM); in case of electronic warfare, electronic counter-counter measure (ECCM) is used.
In case of IR missiles, the aircraft should first know it is being targeted. The usual method is to use a Missile Approach Warning Sensor (MAWS), which detects the missile plume. However, this is not a foolproof method. As far as IR missiles are concerned, the target is usually unaware of the missile lock. The problem with laser guided short range missiles is also quite similar.
A number of newer missiles have dual/multi mode seekers (like IR/UV in Stinger), which helps it differentiate better between the target and decoy. Though decoys can mimic the target in one band (for e.g. IR), it cannot mimic in both the bands (flares have different UV signatures compared to the aircraft).
The missiles can be programmed to ignore the decoy (for e.g chaff) and lock only onto the target signature (rather than locking on to the strongest signal return).
The missiles can differentiate between the target and decoy using the differences in their trajectory.
The missile can discriminate between the target and decoy by using a higher resolution seeker.
It is quite difficult to have a system that couldn't be fooled. Though a video/TV guided missile would come quite close, it is quite impractical in real world. Some laser guided missiles are touted as 'unjammable', but this claim is questionable. Best option is to have a multi-mode seeker that locks onto the target in different regions of the electromagnetic spectrum, though this would make the missile too expensive (again, countermeasures are possible).