A thousand years ago, King Arthur leads his knights and men into a battle they can't hope to win. Having realized this, he enlists the help of the magician Merlin, who negotiates with a Cybertronian Knight. The Knight gives Merlin a staff wielding ultimate power, and he has the gestalt Knight Dragonstorm aid Merlin. Dragonstorm comes to the knights' aid, wiping out their adversaries.
In the present day, Optimus Prime is absent while Transformers remain outcasts on Earth. However, more Transformers have been arriving on the planet recently. The Transformers Reaction Force dedicates itself to hunting down and destroying Transformers of all factions. In an area of war-torn Chicago, a group of kids stumble upon a crashed alien ship with a Cybertronian Knight inside. The kids are confronted by a TRF Walker, which is destroyed by Izabella, a survivor of the Battle of Chicago. Izabella and her Transformer companions, Sqweeks and Canopy, urge the kids to escape, but Canopy is shot by a TRF plane drone and dies. Shortly after, Bumblebee and Cade Yeager arrive and help the kids escape. Yeager finds the dying Knight, who gives him a metallic talisman.
In the last few days, the Transformers' home world, Cybertron, has been approaching Earth. Optimus Prime crash-lands on the planet, awakening him from stasis. Finding that Cybertron is dead, Optimus confronts the creator of his race, a powerful sorceress named Quintessa; she corrupts Prime so he does her bidding — destroy Earth, or "Unicron" the chaos bringer, so Cybertron can live. Lennox, a former N.E.S.T. member who now works under the TRF, speaks with General Morshower, his former superior, about the presence of an ancient alien technology on Earth. This technology can help bring Cybertron into Earth's atmosphere, but only through the use of the staff given to Merlin. The TRF makes a deal with the reborn Megatron, who demands the release of his fellow Decepticons — Onslaught, Mohawk, Nitro, Dreadbot, and Berserker. Megatron and his team go off to find Yeager.
Yeager hides out at a junkyard, which serves as a sanctuary for many of the surviving Autobots — Bumblebee, Hound, Drift, Crosshairs, Grimlock, Wheelie, and many others. While there, Yeager finds that Izabella has followed him back; Yeager tells her to leave but Izabella wants to stay and fight. Megatron's Decepticons arrive at the junkyard and attack the Autobots; in the ensuing chaos, most of Megatron's warriors are killed except Nitro and Barricade. Yeager is confronted by Cogman, a Headmaster, while an Oxford professor named Viviane is kidnapped by the Autobot Hot Rod. Both are brought to England and meet with Sir Edmund Burton, the last living member of the Witwiccan Order. Burton tells Cade and Viviane of the Transformers' origins, saying that the collision of Cybertron with Earth would spell certain doom. He tasks Viviane, the last descendant of Merlin, with obtaining his staff so they can prevent an ancient machine from being activated. Burton tells Yeager that, as a person defined by honor and dignity, he is the last knight. The talisman given to Yeager has wrapped around his arm by this point. The trio are forced to evacuate Burton's home when the TRF arrives.
With Yeager's help, Viviane delves into her father's past, and finds that they must take the H.M.S. Alliance into the sea to find the staff inside the ancient machine. Bumblebee, Yeager, and Viviane go to the Alliance with Cogman and Burton, who declines to go on the journey any further. The Alliance heads for the machine, but is intercepted by a TRF submarine. The Alliance ventures inside the machine, where Viviane and Yeager find a chamber of Cybertronian Knights, as well as Merlin's staff. One Knight, Skullitron, awakens and attacks them. The TRF intervenes and attacks Skullitron, while Viviane activates the staff and the machine rises towards the ocean's surface. As more Knights rise up to fight, Optimus, redubbed "Nemesis Prime" by Quintessa, intervenes, and takes the staff from Viviane with the intention of restoring his homeworld.
Bumblebee confronts and fights Optimus on top of the machine, as Yeager and Lennox struggle to step in. As Optimus is about to kill Bumblebee, he speaks, urging Prime to remember who he is. Optimus breaks free of Quintessa's control, but Megatron flies in and seizes the staff from Optimus. The Cybertronian Knights then attack Optimus for his betrayal, but Yeager — whose talisman becomes the Excalibur sword — stops the fight. The Knights yield to Yeager, who urges Optimus to protect the Earth once more.
At Stonehenge, Megatron works with Quintessa and uses the staff to activate the machine, controlling Cybertron. As the military intervenes, Megatron shoots Burton, who dies by Cogman's side. Optimus unites the Knights with the Autobots and the TRF soldiers. Using Lockdown's ship, they set off to stop Cybertron's collision with Earth. They raid Cybertron and battle against the Decepticons and Infernocons, Quintessa's soldiers. The Autobots and TRF soldiers destroy the Infernocons and guide Viviane and Cade so they can get to the staff. As Quintessa commences Cybertron's resurrection, the Autobots battle Megatron, who is defeated by Optimus but survives. Quintessa is seemingly vanquished by Bumblebee, and Viviane removes the staff — stopping the collision of Cybertron.
The Autobots re-unite after the battle, and Optimus declares that his faith in humanity has been restored. The Autobots use Lockdown's ship to return to Cybertron.
In a mid-credits scene, scientists inspect a part of Unicron. Quintessa, now disguised as a human, arrives and offers them a way to destroy the being.
- Mark Wahlberg as Cade Yeager
- Laura Haddock as Viviane Wembly
- Josh Duhamel as William Lennox
- Isabela Moner as Izabella
- Anthony Hopkins as Sir Edmund Burton
- John Turturro as Seymour Simmons
- Santiago Cabrera as Santos
- Jerrod Carmichael as Jimmy
- Glenn Morshower as Morshower
- Stanley Tucci as Merlin
- Liam Garrigan as King Arthur
- Tony Hale as JPL Engineer
- Peter Cullen as Optimus Prime
- Erik Aadahl as Bumblebee
- Omar Sy as Hot Rod
- John Goodman as Hound
- Ken Watanabe as Drift
- John DiMaggio as Crosshairs
- Jim Carter as Cogman
- Reno Wilson as Sqweeks
- Tom Kenny as Wheelie
- Steve Buscemi as Daytrader
- Steven Barr as Topspin
- Frank Welker as Canopy
- Mark Ryan as "Bulldog"
- Spitfire Autobot
- Frank Welker as Megatron
- Jess Harnell/Frank Welker as Barricade
- John DiMaggio/Steven Barr as Nitro Zeus
- Reno Wilson as Mohawk
- John DiMaggio as Onslaught
- Steven Barr as Berserker
- Starscream's head
- Decepticon Protoforms