In 17,000 B.C., the seven Primes traveled the galaxy to create Energon with star-absorbing machines called Sun Harvesters. The Primes followed a law in which to never kill planets with life, but one of them, later called The Fallen, deceives the others by building an army and sets up a Harvester on Earth. After defeating ancient humans, the Primes defeat and imprison the Fallen before he can harvest the Earth's sun with the "Matrix of Leadership." The rest of the Primes sacrifice themselves to hide the Matrix.
Two years after Megatron's destruction, the Autobots have joined the U.S. military to form the Non-biological Extraterrestrial Species Treaty (NEST), an elite, classified task force to hunt down Decepticon resistance. During a mission in Shanghai, Optimus Prime learns of The Fallen's return, but is unfamiliar with the name. National Security Advisor Theodore Galloway chastises the task force for their destructive tactics and reminds them that Megatron's corpse is still in the Laurentian Abyss, and that the surviving Allspark shard is locked away. Soundwave, the Decepticons' Communications Officer, hacking into a U.S. military satellite, eavesdrops on the conversation and sends Ravage to retrieve the shard from NEST's headquarters.
Meanwhile, Sam Witwicky prepares to attend college, leaving his girlfriend Mikaela Banes and guardian Bumblebee behind. Sam goes on a rampage through the school. Sam manages to get to France, where he discovers another Allspark shard, which channels its knowledge into his mind, causing him to see Cybertronian symbols. He gives the shard to Mikaela, who then overpowers Decepticon Wheelie as he tries to steal it. Decepticons resurrect Megatron using the retrieved shard, who reunites with The Fallen. The Fallen orders Megatron to capture Sam and kill Optimus, who is the last of the Primes and the only Transformer who can defeat him.
Sam, Mikaela, and Leo are targeted by the Decepticons. Alice, a Pretender, first lies down on Sam and attacks him. They kill Alice, but are captured by Megatron, who attempts to take out Sam's brain. Megatron makes Sam lie on a table in an abandoned warehouse and has Scalpel remove his brain, but Optimus and Bumblebee arrive to rescue them. Optimus engages Megatron, Starscream and Grindor in a nearby forest where after losing badly at first, Optimus overpowers Megatron, defeats Starscream and kills Grindor. As Optimus attempts to find Sam, Megatron wounds him. After The Fallen learns of his fall and after Optimus encourages Sam to run, Optimus dies. This forces Sam and his friends to retreat. The Decepticons prepare for the Fallen's arrival, who uses Earth's telecommunications and asks for Sam in return for mankind's survival, making Sam an international fugitive. Sam enlists the help of former Sector 7 Agent Seymour Simmons, who reveals that the Transformers visited Earth eons ago and the most ancient, known as Seekers, live in secret. With help from Wheelie, they track down an elderly Decepticon Jetfire, who explains the Fallen's history, then teleports the group to Egypt to find the Tomb of the Primes and the Matrix to revive Optimus. Simmons contacts NEST, telling them to bring Optimus and the other Autobots to Egypt.
Sam's group finds the tomb hidden in Petra, but the Matrix disintegrates into dust in Sam's hands. Undeterred, Sam places the Matrix's remains in his sock. NEST and the Autobots land, but the Decepticons attack, while other Decepticons demolish one of the Pyramids of Giza, unearthing the Sun Harvester hidden inside. An airstrike is summoned, which kills most of the Decepticon forces; however Megatron manages to shoot Sam, seemingly killing him. Sam has a vision and meets the Seven Primes, who tell him the Matrix is earned, and that he has earned the right to bear it. They revive Sam and restore the Matrix, which Sam then uses to revive Optimus.
The Fallen arrives and steals the Matrix to start the Sun Harvester. A dying Jetfire sacrifices himself in order for Optimus to gain new powers. Optimus knocks The Fallen and Megatron off the pyramid and destroys the Sun Harvester. Optimus engages The Fallen and gets the upper hand until Megatron interferes, though Optimus easily defeats Megatron and cripples him, who calls Starscream for help. The Fallen fights Optimus again and starts to tear his new armour apart but Optimus injures him easily. Wounded, The Fallen attempts to escape but Optimus rips his spark out from his chest, killing him, all while Megatron watches in horror. After agreeing to a suggestion to retreat, Megatron (who is now the leader of the Decepticons once more) vows revenge and retreats with Starscream. The Autobots and their allies then board an U.S. Navy aircraft carrier to return to United States as Optimus sends a message of his own.
- The Powderpuff Girls (Blossom, Bubbles, and Buttercup), Courage the Cowardly Dog, Norbert and Dagget, Rodney Copperbottom, Fender Pinwheeler, Wonderbot, Piper Pinwheeler, Crank Casey, Lug, and Diesel guest star in this film.
- The storyline continues in Ed, Edd, n Eddy's Ed-ventures of Transformers: Dark of the Moon.