In 17,000 B.C., the Seven Primes travel across the universe to create Energon with star-absorbing machines called Sun Harvesters, which destroy stars in order to collect their raw material. One of them defies the rule to never to destroy a planet with life by establishing a Sun Harvester on Earth, earning him the name “The Fallen”. The Fallen is confronted by the other Primes, who imprison him before he can harvest the Sun using the Matrix of Leadership. The rest of the Primes then sacrifice themselves to hide the Matrix in an unknown location.
In the present, two years after the events of the first film, the Autobots and the U.S. military have formed the Non-biological Extraterrestrial Species Treaty (NEST), a classified international task force used to eliminate the surviving Decepticons. In Shanghai, the Autobots intercept two Decepticons, Demolisher and Sideways. Sideways is killed by the Autobot Sideswipe, while Optimus Prime kills Demolisher, but not before he warns them of the Fallen’s return. At NEST’s secure headquarters in Diego Garcia, National Security Adviser Theodore Galloway concludes that since Megatron’s corpse is at the bottom of the Laurentian Abyss and the last-known AllSpark shard is secured in the base, the Decepticons must still be on Earth solely to hunt the Autobots. Unfortunately the Decepticon Soundwave had hacked into a military satellite and so overheard this information, sending Ravage to retrieve the shard.
Meanwhile, Sam Witwicky is preparing to attend college, leaving his girlfriend Mikaela Banes and guardian Bumblebee behind. He finds a smaller AllSpark shard and picks it up, causing him to see Cybertronian symbols. As a side effect, the shard’s energy brings various kitchen appliances to life, who then attack Sam and his family. After Bumblebee kills the living appliances, Sam gives the shard to Mikaela, who later captures the Decepticon Wheelie when he attempts to steal it. The Constructicons resurrect Megatron by brutally ripping apart the Constructicon Scrapmetal to provide parts, as well as using the stolen shard. Megatron then travels to one of Saturn’s moons, where he reunites with his second in command, Starscream, and his master, the Fallen, who orders him to capture Sam alive and kill Optimus, as he is the only Transformer who can defeat the Fallen. After killing Alice, a Decepticon Pretender disguised as a college student, Sam, Mikaela and his college roommate, Leo are captured by the Decepticon Grindor and taken to an abandoned factory. Megatron reveals that the symbols in Sam’s mind will lead the Decepticons to a new Energon source before Optimus and Bumblebee arrive to rescue the trio. Optimus engages and defeats Megatron, Starscream, and Grindor. After the battle, Megatron kills Optimus by impaling him through the chest from behind, destroying his spark. The other Autobots arrive and they force Megatron and Starscream to retreat. The Decepticons launch devastating simultaneous attacks around the world, destroying ships in the Second Fleet and damaging Paris. The Fallen hijacks Earth’s telecommunications systems, demanding that Sam be handed over to him.
Sam, Mikaela, and Leo enlist the help of former Sector Seven agent, Seymour Simmons, who reveals the Transformers visited Earth eons ago and the most ancient, known as Seekers, remained on Earth, hiding in secret. With help from Wheelie, they track down a Decepticon Seeker named Jetfire at the Smithsonian Air and Space Museum. They use their shard to revive Jetfire, who teleports the group to Egypt and explains the story of the Fallen. Following Jetfire’s example, Wheelie sides with the Autobots, and Jetfire sends them to locate the Matrix, which could revive Optimus. Sam’s group finds the Matrix in Petra, but it disintegrates into dust in Sam’s hands. Undeterred, Sam stuffs the Matrix’s remains into his sock.
Meanwhile, NEST forces and the Autobots land near the Giza pyramid complex but are attacked by a large force of Decepticons. During the battle, many Constructicons combine to form Devastator, who destroys one of the pyramids to reveal the Sun Harvester inside before he is killed by a US navy destroyer’s railgun. Ravage and the Decepticon Rampage attempt to spring a trap, using Sam’s captured parents as bait, in order to force Sam to give them the Matrix, but Bumblebee interferes and kills them both. Major William Lennox and Master Sergeant Robert Epps call in an airstrike that kills the majority of the Decepticon ground forces. However, Megatron manages to shoot Sam, mortally wounding him, before Megatron is forced to retreat. As Sam nears death the Primes contact him through a vision, saying that the Matrix must be earned, not found, and that he has now earned the right to bear it. They restore Sam’s life and the Matrix, which he uses to revive Optimus.
The Fallen teleports to their location and steals the Matrix from Optimus, then returns to the pyramid with Megatron and activates the Sun Harvester. Jetfire, who was gravely wounded during the battle, sacrifices himself in order to transplant his parts to Optimus, which gives Optimus immense strength and the ability to fly. He knocks the Fallen and Megatron off the pyramid, destroying the Sun Harvester in the process. In the battle, Optimus incapacitates Megatron and kills the Fallen; Megatron retreats with Starscream, vowing vengeance. The victorious Autobots and their allies then go back to the United States and Sam returns to college.
(all thanks to Wikipedia and to the monomyth)