Quick Answer: Damp air inside a car turns to water on glass that is colder than the air's dew point. The dew point is the temperature where water vapor in the air starts to turn into liquid. Water vapor condenses when it turns from an invisible gas into liquid droplets, and on a window those droplets look like fog.

What the dew point is

The Illinois Physics Van gives the rule simply. Water condenses on a surface that is colder than the dew point of the air next to it. Inside a car, the air next to the glass is cabin air. For the general process, see How Does Fog Form?

Why cold or damp weather causes it

Cold outside air can chill the glass. The Illinois Physics Van says moisture condenses on the inside of the windows. This happens when the outside air is cooler than the dew point inside the car.

Humidity matters too. The Government of Québec's SAAQ page says the higher the humidity, the more fog forms on the windshield.

How air conditioning clears the glass

The Illinois Physics Van says an air conditioner removes water from the air because water condenses on its cold tubes.

The SAAQ says conditioned air works best for defogging, even in winter, because it is drier.

The Michigan State Police advise switching on the air conditioner. They say this may sound odd, but it helps remove moisture from the air. The same page says recirculation set to OFF will reduce fogging while you drive.

How heat and outside air help

Heat helps too. The Illinois Physics Van says heating air well past its dew point makes it dry, which helps evaporate condensation on the windshield.

Outside air can help when it is drier than the air inside the car. It does not always clear fog. The SAAQ advises cracking a window or opening the sunroof slightly so fresh air flows through.

Controls differ between cars. The SAAQ also advises checking the owner's manual.

Sources & further reading

That’s the mechanism. The wonder is still allowed.