Thousands of years ago, there was a race of ancient Transformers who scoured the universe looking for energon sources. Known as the Dynasty of the Primes, they used machines called Sun Harvesters to drain stars of their energy in order to convert it to energon and power Cybertron's All Spark. The Primes agreed that life-bearing worlds would be spared, but in 17,000 BC, one Prime, thereafter dubbed "The Fallen", constructed a Sun Harvester on Earth. The remaining Primes 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 a special forces unit known as NEST (Networked Elements: Supporters and Transformers), a military organization consisting of American and British troops and his own team of Autobots aimed at hunting the remaining Decepticons on Earth. While on a mission in Shanghai, China, 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 finds a splinter of the destroyed All Spark, and upon contact the splinter fills his mind with Cybertronian symbols. Deeming it dangerous, Sam gives the All Spark splinter to his girlfriend Mikaela Banes for safe keeping, and leaves her and Bumblebee behind to go off to college. Upon arrival, Sam meets his college roommate Leo Spitz, who runs an alien conspiracy website, and Alice, a co-ed who makes unusually strong sexual advances on him.