Credit goes to Wikipedia
Thousands of years ago, the 7 Primes traveled the galaxy to create Energon with sun-absorbing machines called Sun Harvesters. The Primes follow a law that no sun can be destroyed if it has life on one of its planets; however, one of the them breaks the law by setting up a Harvester on Earth in 17,000 B.C. after defeating ancient humans. 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 the NEST. During a mission in Shanghai, Optimus Prime learns of The Fallen’s return, but is unfamiliar with the name. They chase remaining Decepticons 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.
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. 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, who reunites with The Fallen, the founder of the 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. Optimus and Bumblebee attempt to rescue them. Optimus kills Grindor but in the ensuing fight, Optimus is killed by Megatron. 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. 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 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 Ravage in the process. Chief Master Sergeant Robert Epps (Tyrese Gibson) summons an air strike, killing most of the Decepticons, but Megatron shoots and kills Sam. In the afterlife, Sam meets the Primes, who tell him the Matrix is not found but earned. They resurrect Sam, who uses the Matrix to revive Optimus. The Fallen appears and uses the Matrix to start the Sun Harvester. Jetfire relinquishes his parts to Optimus, allowing the latter to gain new powers, including flight. Optimus destroys the Sun Harvester, cripples Megatron, and kills The Fallen. Megatron escapes with Starscream, and the Autobots and their allies return to the United States.