Sixty-five million years ago, the alien race known as the "Creators" invade Earth, wiping out most of life on the planet including the dinosaurs with the "Seeds", and cyber-form thousands of planets, including Cybertron. In the present, Darcy Tirrel discovered frozen dinosaur corpses in the Arctic.
Five years after the final battle between the Autobots and Decepticons that leveled Chicago, humanity has grown fearful of the Transformers, resulting in the termination of joint operations with the Autobots. Cemetery Wind, an elite CIA black ops unit formed by opportunistic agent and paranoid government official Harold Attinger and ruthless team leader James Savoy, is tasked with hunting the remaining Decepticons. However, they have also been secretly hunting Autobots, believing all Transformers to be a threat, despite the Autobots officially being granted sanctuary by the government. With the help from the Cybertronian bounty hunter Lockdown, they hunt down, ambush and kill most of the Autobots, including Ratchet and Leadfoot. Optimus Prime is the only Autobot to escape and go into hiding after an ambush in Mexico City, and the primary target whom Lockdown wants captured alive.
In rural Texas, Cade Yeager is an inventor struggling financially, with college funding for his daughter, Tessa, and the loss of his property looming. One day, he and his friend Lucas Flannery discover an old truck in an abandoned theater and they buy it to repair and sell. Cade discovers that the truck is an injured Optimus Prime and repairs him, bringing him back to life. Because Lucas notifies them, Cemetery Wind and Lockdown confront and threaten the Yeagers and Lucas before Optimus attacks and rescues them before Lockdown fires a barrage of missiles and destroys the Yeager's home. Cade, Tessa and Lucas escape with Tessa's secret boyfriend, Shane Dyson. Safety is found after a long chase, but Lucas is killed by one of Lockdown's grenades, while trying to escape. Tensions rise between Cade and the two lovers over their secret relationship while Optimus rallies remaining Autobots Bumblebee, Hound, Drift, and Crosshairs in the desert. Cade hacks into a spy drone he took from the Texas attack, learning of Kinetic Solutions Incorporated (KSI), a technology firm in league with Cemetery Wind, and they decide to infiltrate KSI HQ in Chicago. Before departing however, a fuming Optimus declares he will personally kill Attinger for plotting against the Transformers.
Meanwhile, Joshua Joyce, the arrogant head of KSI, shows Transformium (the scarce metal of which Transformers are made) to Darcy. He has imprisoned Brains to decode dead and fallen Transformers' minds like Megatron, Sentinel, Leadfoot and Ratchet for raw materials and utilize their data for human-created Transformers "upgrades." Joshua shows Darcy his prized creation Galvatron, created using data from Megatron's severed head. Despite designing Galvatron to look like Optimus Prime it instead resembles Megatron. The Autobots storm the facility, but Joshua stops them, proclaiming the Autobots are no longer needed now that they can create their own Transformers. Disillusioned, the Autobots decide to leave.
Forced by Attinger, Joshua launches Galvatron and Stinger (a man-made Transformer similar to Bumblebee) to pursue the Autobots. Optimus engages Galvatron in a brawl which is cut short when Lockdown interrupts the fight, wounding Optimus before capturing him and Tessa on his ship while Galvatron and Stinger draw back. He explains that those who made the Transformers, the "Creators," want Optimus back. Before leaving, Lockdown gives Cemetery Wind a powerful relic called the Seed, which when detonated transforms living things into transformium and cyberforms wide areas of land, giving the Autobots time to board the ship. Cade and Shane save Tessa and escape with Bumblebee and Crosshairs while Hound and Drift escape with Optimus on a detachable part of Lockdown's ship. Optimus and Brains reveal that Galvatron is the reincarnated Megatron who has manipulated KSI to steal the Seed and rebuild the Decepticons. An enraged Joshua demands to know why he did not have any control over Galvatron, finally deciding to move to his factory in Guangzhou, China. Cade warns Joshua, who retreated to Beijing to use production facilities with Su Yueming, about Galvatron and Attinger. Having a change of heart and knowing that Cade is correct, Joshua confronts Attinger over his actions and backs off their deal. But as the two come to an argument, Galvatron activates himself and infects all the KSI prototypes. Joshua flees with the Seed to Hong Kong, with a betrayed Attinger and Galvatron chasing him.
When the Autobots try to retrieve the Seed at Hong Kong, their ship is shot down by the new Decepticons, leaving the Yeagers, Shane, Hound and Bumblebee to fight. Along the way, Cade kills Savoy in a fist fight in an apartment before Attinger contacts Lockdown for the Autobots' current location, where the Decepticons outnumber and outmatch the Autobots. Knowing this, Optimus releases and tames the legendary warriors known as the Dinobots, with Optimus riding Grimlock after defeating him in a fight while pleading for his assistance. With their help, the Autobots defeat the Decepticons while Bumblebee manages to kill Stinger while riding Strafe. Unfortunately, Lockdown, angry that Optimus hijacked part of his ship, returns with a magnetic weapon to reclaim Optimus and the Dinobots. After Optimus destroys the magnetic weapon, he fights Lockdown on an island off Hong Kong. Cade arrives during their fight and attempts to help Optimus, but is attacked and held at gunpoint by Attinger, who proudly justifies his actions before snarling at Cade for siding with the Transformers over humanity (still considering them a threat). Witnessing this, Optimus saves Cade by shooting Attinger, killing him as he promised earlier on. However, the distraction allows Lockdown to overpower Optimus before impaling him with his sword to a wall. Having arrived to help, Bumblebee helps Cade fight Lockdown, only for the villainous bounty hunter to defeat Bumblebee and attack Cade. This is when Tessa and Shane use a tow truck to pull the sword out of Optimus, who surprises and kills Lockdown before he can finish off Cade, avenging the deaths of his fellow Autobots Ratchet, Leadfoot, Sideswipe, Dino, Topspin and Roadbuster in the process.
Everyone escapes after Optimus uses Lockdown's last grenade to destroy the remaining Decepticons while Galvatron, the sole surviving Decepticon, retreats to an unknown location with Lockdown's ship, vowing that he will return for he is reborn. The Dinobots are then set free and they depart to find their own path as Joyce plans to make amends with Cade (who finally accepts Shane as his daughter's boyfriend) by arranging for him and Tessa to gain a new house. Optimus then flies into space with the Seed, sending a message to the Creators that he is coming for them, but after ordering the surviving Autobots to protect the Yeagers.