Thousands of years ago, the 7 Primes traveled the galaxy to create Energon with sun-absorbing machines called Sun Harvesters. The Primes followed a law that no sun can be destroyed if it has life on one of its planets; however, one of them deceives the others by building an army and by breaking the law by setting up a Harvester on Earth in 17,000 B.C. after defeating ancient humans. The Primes come to Earth and defeat and imprison him before he can harvest the Earth's sun. The rest of the Primes Dynasty name him The Fallen, and sacrifice themselves to hide the "Matrix of Leadership", the key to start the Harvester.
Two years after Megatron's destruction, the Autobots have joined the U.S. military to create the Nonbiological Extraterrestrial Species Treaty (NEST), an elite, classified military task force working with the Autobots hunting down Decepticon resistance, with Major William Lennox (Josh Duhamel) and his squad as the human part of NEST. During a mission in Shanghai, Optimus Prime learns of The Fallen's return, but is unfamiliar with the name. They chase the two remaining Decepticons, Sideways and Demolisher through the Shanghai streets, puncturing a bridge. National Security Advisor Theodore Galloway (John Benjamin Hickey) chastises the task force for their destructive tactics and reminds them of the location of Megatron's corpse in the Laurentian Abyss and the surviving Allspark shard retrieved by Optimus. Soundwave, the Decepticons' communications officer, eavesdrops on the conversation and sends Ravage to retrieve the shard from NEST's base. Soundwave later hacks into a military satellite.
Meanwhile, Sam Witwicky (Shia LaBeouf) prepares to attend college, leaving his girlfriend Mikaela Banes (Megan Fox) and guardian Bumblebee behind. Packing his things, he discovers another Allspark shard in his old T-shirt he wore on the day when he destroyed Megatron. The shard channels its knowledge into his mind, causing him to see Cybertronian symbols and bringing the kitchen appliances to life before Bumblebee kills them. He gives the shard to Mikaela, and when the Decepticon Wheelie tries to steal it, Mikaela locks him in a toolbox. At college, Sam meets his roommate, Leo Spitz (Ramón Rodríguez), who runs a conspiracy website regarding the Transformers. Sam's hallucinations get worse, attracting the attention of fellow student Alice (Isabel Lucas), who is really a shapeshifting Pretender Decepticon. Ravage, Scalpel, and the Constructicons resurrect Megatron using the retrieved shard and they kill one of their own to provide parts for him, who then proceeds to chastise Starscream for abandoning him on Earth previously. He then reunites with The Fallen, the founder and master of all Decepticons. The Fallen orders Megatron to capture Sam and kill Optimus, who is the last surviving descendant of the Primes and the only Transformer who can kill him.
Sam, Mikaela, and Leo are targeted by the Decepticons. They kill Alice, but are captured by Megatron, Starscream, and Grindor, who attempt to take out Sam's brain. Optimus and Bumblebee arrive to rescue them. Optimus engages Megatron in a nearby forest, nearly overwhelming him before the other Decepticons intervene. After Optimus kills Grindor by ripping his head to pieces, Megatron takes advantage of the confusion to stab and kill Optimus. Sam and his friends retreat while the Autobots fight off the Decepticons. Megatron and Starscream head to the top of a building to have a quick meeting, in which Megatron tortures his second-in-command for their failures, but decides that his master should arrive now. Hearing this, Soundwave orders all the Decepticons to mobilize. Now pleased at the fact that no one can stop him, The Fallen uses Earth's telecommunications to ask for Sam in return for mankind's survival, making Sam an international fugitive. Sam, Mikaela, Leo, Bumblebee, and twins Skids and Mudflap enlist the help of ex-Sector 7 Agent Seymore Simmons (John Turturro). Simmons 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 the elderly Decepticon Jetfire, who defected to the Autobots years ago, tired of fighting, and he and Wheelie change sides and join the Autobots. Jetfire teleports the group to Egypt and explains The Fallen's history before sending them off to find the Tomb of the Primes 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 crumbles to dust in Sam's hands. Undeterred, Sam places the Matrix's remains in his sock. NEST and the Autobots land, but are cut off from the Pentagon thanks to Starscream. The Decepticons attack, including Constructicon Devastator (who is made up of smaller Decepticons) who demolishes one of the Pyramids of Giza, unearthing the Sun Harvester, which was hidden inside the pyramid. He is then killed by a railgun attack summoned by Simmons. Sam's parents, who were kidnapped by the Decepticons, are used as bargaining chips with Sam to try to obtain the Matrix, but the trap fails when Bumblebee rescues them, killing the Decepticons Rampage and Ravage in the process. Jetfire arrives to join the fight and is severely wounded by Scorponok, but is still able to kill him. Chief Master Sergeant Robert Epps (Tyrese Gibson) summons an airstrike which kills most of the Decepticons; however Megatron manages to shoot and seriously injure Sam. While fighting for life, Sam has a vision, in which he meets the Primes, who tell him the Matrix is not found but earned, and that he has earned the right to bear it. They then revive Sam and restore the Matrix, which Sam then uses to resurrect Optimus.
The Fallen appears and steals the Matrix so that he can start the Sun Harvester. A dying Jetfire sacrifices himself and relinquishes his parts to Optimus, allowing the latter to gain new powers, including flight. Optimus uses his new powers to knock The Fallen and Megatron off the pyramid and destroy the Sun Harvester. Optimus then engages The Fallen in a duel, in which he seems to gain the upper hand until Megatron intervenes. Optimus forces Megatron's hand-cannon to fire into his own face, cuts off his arm, and sends him flying through a wall with a reverse blast from the jet thrusters taken from Jetfire. Megatron, wounded, calls for Starscream in agony. The Fallen re-engages Optimus, ripping off parts of his armor and seriously wounding him with his spear, but Optimus rips the spear from the Fallen's grasp and tears off part of the outer shell on his face. The Fallen dies after his spark is ripped from his chest by Optimus. As Megatron escapes with Starscream, the Autobots and their allies return to the United States.