Four years have passed since the final battle between the Autobots and the Decepticons left Chicago in ruins and claimed the lives of over a thousand civilians. The U.S. government has severed its ties with the Autobots and branded them as fugitives. An elite CIAunit called "Cemetery Wind" is formed by Harold Attinger with the intent of hunting down and exterminating the surviving Autobots with the aid of Lockdown, a Transformer bounty hunter. Meanwhile, using data obtained from destroyed Transformers, business tycoon Joshua Joyce and his technology firm Kinetic Solutions Incorporated (KSI) have discovered "Transformium", the molecularly unstable metal that is the lifeblood of Transformers. Joshua's prized creation is Galvatron, a Transformer created from the data inside Megatron's severed head.
In rural Texas, struggling robotics inventor Cade Yeager and his friend Lucas Flannery purchase an old semi-truck in hopes of stripping it down and selling the parts to get Cade's daughter Tessa into college. Cade discovers that the truck is an injured Optimus Prime, and it is not long before Lockdown and Cemetary Wind operatives led by James Savoy storm into the Yeagers' farm and threaten them. Optimus comes out of hiding to fend off against the operatives while Cade, Tessa, and Lucas are rescued by Tessa's boyfriend Shane Dyson, an Irish rally racer. They lose the operatives in a lengthy chase in Paris, Texas, but Lucas is killed by Lockdown's grenade during their escape. Using a drone he took during the raid, Cade discovers that the operatives and KSI are working together. Optimus rallies the remaining Autobots - Bumblebee, Hound, Drift, and Crosshairs - and travel with their new human allies to infiltrate KSI's headquarters in Chicago. There, Cade, Shane, and Bumblebee discover the firm's reverse engineering of Transformer technology. Upon discovering that Ratchet has been slain and his head is being melted down, Optimus and the Autobots storm into the headquarters to destroy the laboratory, but Joshua convinces them that their actions are futile. As the Autobots leave the premises, Joshua launches KSI's prototype Transformers Galvatron and Stinger. Optimus and Galvatron engage in a grueling battle where Optimus realizes that Galvatron is Megatron in a new body. Suddenly, Optimus is blasted from behind by Lockdown, and in the midst of the chaos, he and Tessa are captured into Lockdown's ship. Aboard the ship, Lockdown explains to Optimus that the Transformers were created by a mysterious alien race known as the "Creators", which hired him to capture the Autobot leader. As a reward for Optimus' capture, Attinger's operatives are given the "Seed", a powerful bomb that turns a city-wide area of land into Transformium. Cade, Shane and the Autobots storm into the ship to rescue Optimus and Tessa; while Bumblebee, Crosshairs and the humans escape and crash into Downtown Chicago, the other Autobots detach the rear section of the ship before it leaves into space. Joshua and his business partners Su Yueming and Darcy Tyril retreat to Beijing, where Attinger hands Joshua the Seed in exchange for a stake in KSI control. The Autobots and their human allies follow them to prevent them from detonating the Seed.
At KSI's Beijing factory, Galvatron suddenly activates by himself and infects all 50 of the firm's prototype transformers to do his bidding. Realizing the folly of his creations, Joshua betrays Attinger before he, Yueming, and Darcy take the Seed to Hong Kong. There, the Autobots struggle to protect Joshua and the Seed from Galvatron and his minions, which shoot down the Autobots' ship. Cade kills Savoy during a fight in an apartment building. Outnumbered and outmatched, Optimus Prime releases a group of legendary knights and leads them back to the city to destroy Galvatron's army. Lockdown returns to Earth and uses a magnetic weapon to pull vehicles under his ship and drop them on the Autobots. Optimus destroys the weapon and engages in battle with Lockdown before killing Attinger to save Cade. Lockdown grabs Optimus' sword and impales him, but the combined efforts of Bumblebee, Cade, Tessa, and Shane distract the bounty hunter before Optimus stabs him in the heart and slices his head in half. Galvatron retreats, vowing to battle Optimus another day. With Lockdown destroyed and Cemetery Wind dissolved, Optimus sets the Dinobots free before flying into space with the Seed, sending a message to the Creators.