It is revealed that thousands of years ago there was a race of ancient Transformers who scoured the universe looking for energon sources. Known as the Dynasty of Primes, they used machines called Sun Harvesters to drain stars of their energy in order to convert it to energon and power Cybertron's AllSpark. The Primes agreed that life-bearing worlds would be spared, but in 17,000 BC, one brother, thereafter dubbed "The Fallen", constructed a Sun Harvester on Earth. The remaining brothers thus sacrificed their bodies in order to hide the Matrix of Leadership the key that activates the Sun Harvester from The Fallen, who swore to seek revenge upon Earth.
In the present day, two years after the events of the previous film, Optimus Prime is seen leading NEST, a military organization consisting of human troops and his own team of Autobots (including newcomers Arcee, Chromia, Elita One, Sideswipe, Jolt, and the twins Skids and Mudflap) aimed at killing the remaining Decepticons on Earth. While on a mission in Shanghai, Optimus and his team destroy Decepticons Sideways and Demolishor, being given a warning by the latter that "The Fallen will rise again". Back in the United States, Sam Witwicky (Shia LeBeouf) finds a splinter of the destroyed AllSpark, and upon contact the splinter fills his mind with Cybertronian symbols. Deeming it dangerous, Sam gives the AllSpark splinter to his girlfriend Mikaela Banes (Megan Fox) for safe keeping, and leaves her and Bumblebee behind to go off to college taken by his father (Kevin Dunn) and mother (Alice White) after their home was destroyed by some small decepticons created by the splinter. Upon arrival, Sam meets his college roommate Leo Spitz (Ramon Rodriguez), who runs an alien conspiracy website, and Alice (Isabel Lucas), a co-ed who makes sexual advances on him. Everything is chaotic, and Sam's mother gets high on some "special brownies". Back home, Decepticon Wheelie tries to steal the shard, only to be captured by Mikaela.
Decepticon Soundwave hacks into a US satellite and learns the locations of the dead Decepticon leader Megatron and another piece of the AllSpark. The Presidential advisor Gallaway (John Benjamin Hickey) suggests that the Decepticons are on Earth only to hunt down some Primes, so it'd be better for humans if all Primes left, and he says that the President of the USA is thinking about taking out their asylum. Sergeant Epps (Tyrese Gibson) defends the Primes. What if they leave and Decepticons still attack the Earth?, but the advisor tells him to shut up.
The Decepticons retrieve the shard and use it to resurrect Megatron, who flies into space and is reunited with Starscream and his master, The Fallen in the Nemesis. The Fallen instructs Megatron and Starscream to capture Sam in order to discover the location of the Matrix of Leadership.