It is revealed that thousands of years before Optimus Prime and the other Transformers arrived on Earth, there was a race of ancient Transformers who scoured the universe looking for Energon sources. Known as the Dynasty of Primes, they used a weapon called the Sun Harvester 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 one brother, who was thereafter dubbed "The Fallen", constructed a Sun Harvester on Earth in 17,000 BCE. 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 once he found the key.
In the present day, two years after the events of the first film, Optimus now leads NEST, a military organization consisting of human troops and his own team of Autobots (including newcomers Arcee, 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, only to be given a warning: "The Fallen will rise again". Back in the United States, Sam Witwicky finds a splinter of the destroyed AllSpark, and upon contact the splinter fills his mind with Cybertronian symbols, which he is initially unaware contain a riddle that leads to the location of the Matrix of Leadership. Sam gives the AllSpark 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 sexual advances on him. Back home, Decepticon Wheelie tries to steal the shard, only to get captured by Mikaela. After having a mental breakdown, uncontrollably writing in Cybertronian language, Sam calls Mikaela, who immediately leaves to get to him.
Decepticon Soundwave hacks into a US satellite to eavesdrop on the NEST forces, learning the locations of the dead Decepticon leader Megatron and another piece of the AllSpark. 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. The Fallen instructs Megatron and Starscream to capture Sam in order to discover the location of the Matrix of Leadership. Mikaela arrives in town just as Alice, who is revealed to be a Pretender, attacks Sam. Mikaela, Sam, and his roommate Leo drive off and destroy Alice, but are captured by Decepticon Grindor. The Decepticon known as "The Doctor" prepares to remove Sam's brain, but Optimus and Bumblebee appear and rescue him. Sam and Optimus are pursued by Megatron into a forest where Optimus engages Megatron. Megatron is then joined by Grindor and Starscream. In the ensuing battle, Optimus holds the upper hand and kills Grindor and rips off Starscream's arm. In the end, however, Optimus is impaled through the chest by Megatron and dies. With his dying words, Optimus tells Sam to run and the Autobots come to Sam's rescue.
After Prime's death, 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 "RoboWarrior" may be of assistance. "RoboWarrior" is revealed to be former Sector Seven agent Simmons, who informs the group that the Transformers have been on Earth for a long time, as their language is written on ruins all over the world. Wheelie identifies the language as that of the Primes, and directs the group to a former Decepticon named Jetfire, who can translate the language. They find and reactivate Jetfire, who teleports the group to Egypt and translates the symbols, indicating that the tomb of the Primes is located somewhere in the surrounding desert, and only a Prime can kill The Fallen. By following the clues, the group locate a tomb that embeds the Matrix. Upon arriving at the tomb they expected would contain the Matrix, the group seem to find nothing there. Mudflap and Skids then argue, and their fighting accidentally opens a path to a chamber where they find the Matrix, which 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 to bring the other Autobots and Optimus' body.
The military arrives with the Autobots, but so do the Decepticons, and a battle arises. During the fight, Decepticon Devastator is formed and unearths the Sun Harvester inside one of Egypt's pyramids before being destroyed by the US military. Jetfire arrives in time to save the group from Mixmaster, but is mortally wounded by Scorponok. The Air Force carpet bombs the Decepticons, but Megatron breaks through the offensive and kills Sam.
In a vision, Sam meets with the other Primes, who tell him that the Matrix of Leadership is not found but earned, which Sam has done, and send him back. The dust reassembles to form the Matrix, which Sam uses to revive Optimus. The Fallen steals the Matrix shortly after that and activates the Sun Harvester. In his final moments, Jetfire volunteers his parts and spark, giving Optimus the power he needs to defeat The Fallen. With enhanced capabilities, Optimus destroys the Sun Harvester and takes on Megatron and The Fallen, killing The Fallen. Megatron and Starscream retreat and vow that their fight is not finished.