In 484 AD, King Arthur’s wizard Merlin forges an alliance with the Knights of Iacon, a group of twelve Transformers who have hidden on Earth. The knights give Merlin an alien staff, and combine into a dragon to help Arthur triumph over the Saxons.
In the present day, most of the governments on Earth have declared Transformers illegal, and the multinational Transformer Reaction Force (TRF) has been formed to eliminate the alien robots. Despite the absence of Optimus Prime (who left the planet to search for his creator), new Transformers continue to arrive regularly; the newest ship to arrive crash-lands in Chicago, where it is found by a group of children. When a TRF mecha confronts the kids, they are saved by Izabella, a survivor of the Battle of Chicago, and her Transformer companions Sqweeks and Canopy, but Canopy is killed by the TRF in the process. Bumblebee and Cade Yeager arrive and help them escape, but Yeager is unable to save the Transformer, Steelbane, in the ship. Before he dies, Steelbane attaches a metallic talisman to Yeager’s body—an act observed by Decepticon Barricade, who reports to his leader Megatron.
On the far reaches of the Solar System, Optimus Prime discovers that the Transformers’ home world, Cybertron, now disassembled into pieces, is heading directly for Earth. Optimus finds the being in control of Cybertron’s movement, a sorceress named Quintessa, who professes to be the maker he is searching for. The staff which the knights gave to Merlin was stolen from Quintessa, and using her powers, she places Optimus under her control, dubs him “Nemesis Prime,” and charges him with recovering it. Earth, she reveals, is actually Cybertron’s “ancient enemy” Unicron, and she intends to drain his life force so that Cybertron can be restored.
TRF member and former Autobot ally William Lennox brokers a deal between the TRF and Megatron, releasing from their custody a squad of Decepticons who will help Megatron recover the talisman from Yeager. The Decepticons hunt Yeager to his junkyard hideout in South Dakota, where he and many of the surviving Transformers are holed up. During the chaos of the ensuing battle, Yeager is approached by Cogman, the Transformer envoy of British Lord Sir Edmund Burton, who takes him and Bumblebee to England to meet his master. There, Yeager also meets Viviane Wembly, an Oxford professor, who Burton has had the Autobot Hot Rod kidnap. Burton explains that he is the last living member of the “Witwiccan” order, an ancient brotherhood dedicated to guarding the secret history of Transformers on Earth. He also reveals that Viviane is the last descendant of Merlin, and must find and use his staff to prevent the impending destruction of Earth by Cybertron.
Fleeing the TRF, Yeager and Wembly follow clues left by the latter’s father that lead them, Bumblebee, and Cogman to take the submarine HMS Alliance into the sea to find the Cybertronian Knights’ sunken ship, in which they discover the tomb of Merlin and the staff. Wembly activates the staff, and the ship rises to the surface; the TRF arrives to confront the group, but several knights awaken and attack them. The attack is cut short by the arrival of the mind-controlled Optimus, but fortunately, when the normally-mute Bumblebee is finally able to speak, the sound of his voice is enough to break Prime free of Quintessa’s control. A moment later, Megatron arrives to steal the staff; he too has been working for Quintessa all along. As Megatron flees with his prize, the knights attack Optimus for his betrayal, but Yeager, whose talisman becomes the sword Excalibur, stops the fight. Realizing he is the last knight, the knights yield to Yeager, who urges Optimus to protect the Earth once more.
Megatron delivers the staff to Quintessa, who begins draining the life force of Earth/Unicron via Stonehenge. When the military intervenes, Megatron shoots Burton, who dies with Cogman at his side. Using a ship procured by the Autobot Daytrader, the Autobots arrive to join the fight, landing on Cybertron and battling against the Decepticons and Quintessa’s Infernocons. Optimus and his Autobots, backed up by the knights in their dragon form, vanquish their many foes. Optimus defeats Megatron while Bumblebee appears to slay Quintessa. Wembly removes the staff, stopping Cybertron’s destruction of Earth, but leaving the two planets connected. Optimus declares that humans and Transformers must work together to rebuild their worlds, and sends a message calling any surviving Autobots to come home.
In a mid-credits scene, scientists inspect one of the horns of Unicron, which is extending out of the desert. Quintessa, who has survived and is disguised as a human, arrives and offers them a way to destroy Unicron.