Detailed answer
The flatiron heats up, because the heating element (a conductor with a high resistance, which runs through the soleplate) transforms electrical energy into thermal energy. Therefore heat is only produced when the electri circuit is closed.
A clever switch is built into this circuit: A bimetallic strip. This strip buckles when it is exposed to heat. When the temperature crosses a specific threshold, the strip bends so far that it loses its contact to one side. The circuit is open and the temperature begins to decline.
But it only declines a little bit, because the bimetallic strip bends back again when it cools down, so that the circuit closes again. Then the process starts all over again.
Through this clever switch, the temperature of the iron adjusts itself and stays at the right value.

By the way: If the iron has a step switch to set it up to different temperatures, it is usually done by applying different voltages onto the bimetallic strip. Through this it has to bend farther (for higher temperature settings) or less far (for lower temperature settings) to close the circuit.