This fifth Transformers film opens in England during the Dark Ages. King Arthur (Liam Garrigan) and his knights are all in the middle of an epic battle, but their side is losing and being crushed by gigantic fireballs. Arthur awaits his last hope, his chief adviser and wizard Merlin (Stanley Tucci), to save them. The other knights dismiss Merlin as nothing more than a drunk. After downing his drink, Merlin finds a Cybertronian Knight and asks for his help in the fight. The Knight presents Merlin with a powerful staff that will allow him to control Dragonstorm. Merlin wields the staff, which brings Dragonstorm down to help Arthur and his army lay waste to their enemies.
In the present day, Optimus Prime (voice of Peter Cullen) has been lost in his mission to seek out his creators. He is seen floating through space while frozen.
On planet Earth, the Transformers have been deemed illegal since the battle in Hong Kong (in the fourth film). Most of the Autobots have gone into hiding, with Topspin hiding out somewhere in Cuba with Agent Simmons (John Turturro). The Transformers Reaction Force, led by Santos (Santiago Cabrera), has been active for the last few years to eliminate any Transformers they come across.
A NASA engineer (Tony Hale) detects something heading toward Earth in a matter of three days. It's the planet Cybertron.
In Chicago, a group of kids run toward the city's ruins and find a pod containing a Cybertronian Knight. Moments later, a TRF sentinel stomps around and attacks the kids. The sentinel is destroyed by an Autobot named Canopy, belonging to a young girl named Izabella (Isabela Moner), who also takes care of a little Autobot named Sqweeks (voice of Reno Wilson). A TRF drone then strikes Canopy down. Despite Izabella pleading with Canopy to stay strong, he dies. Bumblebee then arrives to take out the drone. He is followed by Cade Yeager (Mark Wahlberg), who has been in hiding for siding with the Autobots. Not long after, Santos and his men show up to try and apprehend Cade. Bumblebee splits himself into multiple pieces to fight back against the TRF agents. Cade then has Santos pinned to the ground with a gun aimed at him, but he is stopped by William Lennox (Josh Duhamel), who now works with TRF. Lennox talks Cade out of doing anything to Santos. After their confrontation, Cade goes to the Knight in his pod. The Knight dies, but not before giving Cade a talisman that grants him a special power. Meanwhile, the Decepticon Barricade has witnessed all this while disguised as a police cruiser.
Optimus crash-lands in what's left of Cybertron. He is held down by the Infernocons. Optimus is approached by Quintessa (voice of Gemma Chan), who is apparently one of Cybertron's creators. Optimus tries to go after her, but she is able to hold him back as they both claim that the other has betrayed their fellow Transformers. Quintessa has Optimus under her control, and she tells him that Earth is really a disguised form of Cybertron's great enemy Unicron. She corrupts Optimus and tells him that in order for Cybertron to live, Earth must be destroyed. Quintessa uses this to give Optimus a chance at redemption, which he accepts.
Back on Earth, Lennox reports to his superior, General Morshower (Glenn Morshower), about the impending impact with Cybertron. They learn that the newly-resurrected Megatron (voice of Frank Welker) is seeking the talisman that Cade has. The TRF reluctantly makes a deal with Megatron to release his fellow Decepticons from an underground prison. Megatron requests Mohawk, Nitro Zeus, and Onslaught. Megatron also requests Dreadbot and Berserker, but the TRF thinks they are too risky to be let out.
In England, Oxford professor Viviane Wembley (Laura Haddock) is playing a game with her colleagues and she wins, but is frequently reminded by her male colleagues, plus her mother and mother's friends, that she's single. Viviane leads a tour in an Arthurian museum, telling her students about the lore and myth surrounding these stories, and bluntly saying that it's all bullshit.
Cade goes to a junkyard where his friend Jimmy (Jerrod Carmichael) is helping him hide the Autobots - Hound (voice of John Goodman), Drift (voice of Ken Watanabe), Crosshairs (voice of John DiMaggio), Wheelie (voice of Tom Kenny), Daytrader (voice of Steve Buscemi), and Grimlock. Jimmy complains that Grimlock is hard to handle. Cade has been followed by Izabella and Sqweeks. Although Cade tries to turn her away, she tells him that she has nowhere to go because Decepticons killed her family, so all she had were Canopy and Sqweeks, and now Canopy is dead. Cade allows her to stay. He then goes to call his daughter Tessa (Nicola Peltz) while she is at college, but he is unable to speak for fear that he may be found by TRF. Tessa tells him that she is fine and that she loves him, but also that he needs to find himself a girlfriend.
Megatron and his goons arrive at the junkyard to find the talisman and kill the Autobots. The Autobots come out of hiding and start fighting the Decepticons. Onslaught and Mohawk are both quickly blasted to pieces. As Cade runs away, he is found by a small humanoid Transformer named Cogman (Jim Carter). He is looking for Cade but nearly loses him when Cade is attacked by a drone. He still gets Cade to go with him eventually.
Viviane is taken by the Autobot Hot Rod (Omar Sy) to the mansion of Sir Edmund Burton (Anthony Hopkins), where she also meets Cade and Cogman. Burton explains to Cade and Viviane the legacy of the Witwiccan Order, having started since the time of King Arthur. Throughout history, the Transformers have been working in the background to help humanity, and the Witwiccans (including Sam Witwicky) have been doing their part to keep the Transformers protected. In a flashback, we see the Transformers helping American soldiers fight against the Nazis in World War II. Burton also mentions the Staff of Merlin, which only a descendant of Merlin's can wield. He reveals to Viviane that she is the last descendant, and her father had kept this a secret from her up until his death. She needs to wield it to prevent something catastrophic from hitting Earth. Burton then explains the talisman, which has wrapped itself around Cade. It chose him because it considers him an honorable man.
TRF agents show up and force everyone out of Burton's mansion. Hot Rod holds them off for a brief moment by trapping them in a time-contained bubble.
Cade, Viviane, Cogman, Burton, and Bumblebee locate the old submarine HMS Alliance with the help of Simmons (who wants to be an honorary member of the Order) to help them locate the Staff. They are taken to the bottom of the ocean where they find a machine and the Staff in a chamber of Knights. The Knight Skullitron awakens and attacks everyone. The other Knights rise and start to fight just as TRF soldiers show up as well. Viviane goes for the Staff, which brings the machine up to the surface.
Optimus shows up to take the Staff and give it to Quintessa so that she can activate the machine and use it to take Earth's core energy and transfer it to Cybertron, which would fry all human life. Optimus then fights Bumblebee while still under Quintessa's control. Before Optimus can strike a fatal blow, Bumblebee's voice comes back at the most convenient time to snap Optimus out of his control and remind him who he really is. Although Optimus is good again, the other Knights attempt to attack for his attempted betrayal. Cade steps in with the talisman, which (somehow) gives him the power to wield Excalibur. The Knights yield to Cade.
The final battle comes to Stonehenge, where Megatron and Quintessa initiate their attack on Earth. Cybertron begins to tear through various parts of Earth. The TRF decide to join forces with the Autobots. Also arriving to the fight are Jimmy, Izabella, and the rest of the Autobots. In the chaos, Megatron kills Burton, who dies with Cogman by his side. As the heroes attempt to stop the machine, Quintessa sends out the Infernocons and a massive gun shooting at them. Lennox tries to pull everyone back as it is way too risky. Izabella sends Sqweeks out to destroy the gun, giving everyone a chance to fight. The Infernocons go after Optimus, but he decapitates all of them with one swift motion.
Within Cybertron, the Autobots fight against Megatron while Viviane attempts to regain the Staff. Optimus chops off Megatron's arm before he kills again, and then Optimus kicks Megatron out and sends him plummeting back to Earth. Viviane nearly falls to her doom but Hot Rod freezes time again and helps her get closer to the Staff. Optimus confronts Quintessa, who holds him back again until Bumblebee comes from behind and seemingly destroys her. Viviane then takes control of the Staff and stops the machine, saving the Earth.
Earth is saved once again. Cade and Viviane make plans to go on a date, and Cade takes Izabella in so she can have a real family again. Optimus sends a call to all Autobots as he realizes that Earth has been and always will be his home.
During the closing credits, a team of scientists are gathered around a site. Quintessa is alive and takes a human disguise to approach one of them, saying she has a way to destroy Unicron. We then see what appears to be a massive mechanical horn sticking out of the ground.