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.
After having a mental "incident" when he starts uncontrollably writing in Cybertronian language at a party,Sam continues acting strange including putting Professor Colan (Rainn Wilson) to shame in the middle of a lesson. In a panic, Sam calls Mikaela, who immediately leaves to get to him.
With Sam's outbreaks worsening, Mikaela arrives at campus just as Alice is revealed to be a Decepticon Pretender attacks Sam. An irate Mikaela -who caught Sam kissing Alice on a bed-, Sam, and his roommate Leo drive off, destroying Alice, but are seized by the Decepticon Grindor.
The Decepticon known as "The Doctor" prepares to remove Sam's brain, but Optimus and Bumblebee turn up and rescue him. In an ensuing fight, Optimus engages Megatron, Grindor and Starscream. Optimus manages to kill Grindor and rip off Starscream's arm, but during a momentary distraction while searching for Sam, he is blindsided then impaled and blasted through the chest by Megatron and dies. Megatron and Starscream depart as the Autobot team arrives to rescue Sam, unable to save Optimus.
After Prime's death, The Fallen is freed from his captivity and Megatron orders a full-scale assault on the planet. The Fallen speaks to the world and demands they surrender Sam to the Decepticons or they will continue their attack. Sam, Mikaela, Leo, Bumblebee, the twins and Wheelie regroup, and Leo suggests his online rival "Robo-Warrior" may be of assistance. "Robo-Warrior" is revealed to be former Sector 7 agent Simmons (John Turturro), who informs the group that the symbols should be readable for a Decepticon. Mikaela then releases Wheelie, who can't read the language, but identifying it as that of the Primes, directs the group to a Decepticon seeker named Jetfire.
They then find Jetfire at the F. Udvar-Hazy Center and reactivate him via the shard of the AllSpark. After teleporting the group to Egypt, Jetfire explains that only a Prime can kill The Fallen, and translates the symbols, which contain a riddle that sets the location of the Matrix of Leadership somewhere in the surrounding desert. By following the clues, the group arrive at the tomb where they ultimately find the Matrix, but it crumbles to dust in Sam's hands. Believing the Matrix can still revive Optimus, Sam collects the dust and instructs Simmons to call Major William Lennox (Josh Duhamel) to bring the other Autobots and Optimus's body.
The military arrives with the Autobots, but so do the Decepticons, and a battle ensues. During the fight, Decepticon Devastator is formed and unearths the Sun Harvester from inside one of the pyramids before being destroyed by the US military with the help of agent Simmons. Jetfire arrives and destroys Mixmaster, but is mortally wounded by Scorponok. The Air Force bombs the Decepticons, but Megatron breaks through the offensive and kills Sam. While dead, Sam is contacted by the Dynasty of the Primes who, acknowledging his courage and dedication to Optimus, revive him and rebuild the Matrix of Leadership. Sam goes on to revive Optimus just in time before the Fallen ambushed him and his allies, slaughtering a few soldiers and takes off with the Matrix to activate the harvester. Jetfire sacrifices himself to have Optimus use his parts to fly to the harvester and successfully destroys it. Then Optimus engages the Fallen in the ruins and kills him by punching through the chest and ripping his spark out. Although the battle was over, Megatron refuses to surrender by fleeing back to the Nemesis with Starscream.
The film ends with Optimus, who is alongside Sam on an aircraft carrier, sending a message into space saying that the humans and Transformers both share a common past. During the credits, Sam is seen returning to college.