Back in Cybertron, the silicon-based life forms there eventually developed the ability to change between two shapes, their normal humanoid forms and the form of a vehicle. Those capable of this change were called Transformers, and were the main units in the civil war that ravaged Cybertron.
After an accident, a cybertronian spaceship called The Ark fell on Earth, and remained dormant for millions of years. Eventually it was disturbed and powered up. Following its programming, it sent out probes to scan for lifeforms. Earth's vehicles were considered the dominant life form, and the Ark proceeded to adapt the Transformers within it so their changed shapes would let them go about incospicous.
A few exceptions include prematurely-activated robots that were changed to resemble the dominant species of 70 million years ago, the Dinobots, and the Decepticon commander Shockwave, whose alternate form is still a space cannon.