Sixty-five million years ago, during the Cretaceous period, beings known as the "Creators" detonate "Seeds", bombs that cyberform large areas of land and anything it touches, wiping out most life on Earth. In the present, a geologist named Darcy Tirrel discovers two dinosaur corpses covered in a strange metal in the Arctic.
Five years have passed since the Battle of Chicago and humanity has grown fearful of Transformers. The U.S. military has severed joint combat operations with the Autobots. Cemetery Wind, an elite CIA black ops unit headed by a paranoid agent Harold Attinger and team leader James Savoy, has been tasked with hunting down the remaining Decepticons. While the Autobots are officially granted sanctuary, Cemetery Wind has been secretly hunting down the Autobots as well, believing them to be a threat, and has destroyed most of them. With the aid of the Transformer bounty hunter Lockdown, they ambush and brutally kill Ratchet. Their primary target, however, is Optimus Prime, whom Lockdown personally wants alive.
Meanwhile in rural Texas, Cade Yeager, a struggling inventor, and his friend Lucas buy an old truck to strip it for parts in hopes of sending Cade's daughter Tessa to college. Cade soon discovers that the truck is an injured Optimus Prime and repairs him after unexpectedly bringing him back to life. After locating Optimus in Texas, Cemetery Wind and Lockdown travel there and confront Cade, Tessa, and Lucas. Optimus comes out of hiding and attacks the operatives, allowing Cade and his friends to escape before their home is destroyed. They are saved by Tessa's secret boyfriend, Shane Dyson, with whom they escape on Shane's race car while Optimus follows. After a long chase through the city, Lockdown throws a grenade and while Cade, Tessa, and Shane survived, it turns Lucas into a horrific metal corpse, killing him instantly. While hiding in an old gas station, tensions between Cade, his daughter, and Shane rise due to the fact that the two teenagers kept their relationship in secret. Optimus, meanwhile rallies the last remaining Autobots: Bumblebee, Hound, Drift, and Crosshairs. Cade hacks into a drone he took from the attack in Texas, and shows them footage of the ambush and the death of Ratchet and Leadfoot, and learns that Cemetery Wind is working with a robotic corporation called KSI. They decide to infiltrate KSI headquarters in Chicago for information, before an angered Optimus vows to kill Attinger for causing all of this and no longer agrees to protect humans.
Meanwhile, Joshua Joyce, the head of KSI, shows Darcy that he has perfected Transformium, the codeable, molecularly unstable metal that the Transformers are composed of. He has imprisoned Brains (who survived the battle from the third film) to decode dead Transformers' brains and utilize their data for human-created Transformers. Joyce shows Darcy his prized creation, Galvatron, who was created using data from Megatron's brain. Optimus and the Autobots storm the facility, free Brains, and start to destroy the facility. Joyce stops them, and when he explains that humans don't need the Autobots anymore, now that they can create their own Transformer soldiers, and the Autobots leave.