On Gallifrey in the distant past, two bemused technicians receive an alert that someone is stealing a TARDIS from the repair bay.
Somewhere in time, Clara keeps running and encounters the Doctor, saving him again... and again... and again.
In 1893 London, a serial killer named Clarence DeMarco is in his cell awaiting execution, and muttering about the coming of the Whisper Men. Vastra, the detective who brought Clarence to justice, receives word that he wants to see her and visits him in jail. He offers her valuable information in return for his life, and says that he heard whispers of the Doctor's secret, a secret that he will take his to grave... and that someone has learned that secret.
Vastra returns home and tells Jenny what Clarence said. The Silurian decides to investigate the matter further and tells Jenny to prepare for a conference call. As Jenny turns away, she catches a glimpse of someone outside the window, and hears whispering. Before she can investigate, Vastra interrupts to ask her where Strax is, and Jenny says that he's taking a weekend off on holiday.
In Glasgow, Strax gets into a barfight with a worker, Archie. As they fight outside in the alleyway, a messenger boy arrives with a telegram from Strax, saying that she's preparing a conference call. Strax wearily tells Archie to beat him over the head until he's unconscious.
In London, Vastra and Jenny sit at a table and light a candle. The fumes cause them to pass out and they dream that they are in an elaborate conference room. Strax appears and Vastra tells him that they're expecting two more women to join them.
In the present day, Clara is at the Maitland home making souffles for Artie and Angie. They're not very happy with her cooking. Clara discovers that someone has sent her a letter, with a note on the outside saying to read it alone. She goes to her room and reads it, and discovers that it's from Vastra. Vastra explains that she has enclosed a candle to put her to sleep. However, the Silurian also dosed the paper with the same drug in case Clara didn't trust her, and Clara collapses to the floor.
Clara wakes up in the conference room along with the last guest: River Song. River is able to control the dream and summons a glass of champagne, and Vastra introduces her and Clara to each other. The Silurian then explains that Clarence provided her with a set of space-time coordinates in return for his life. The coordinates indicate the location of the Doctor's greatest secret, but Jenny admits that they don't know what it is because it's a secret. When Clara wonders whether the Doctor has any secrets, Vastra asks her if he's ever told her his real name.
Jenny senses something in the real world but dismisses it as her imagination. Meanwhile, River tells the others that the Doctor told her his name, but she hasn't spoken with him in a long time. When Clara wonders why they should believe Clarence, Vastra says that he mentioned the name of the planet at the coordinates: Trenzalore. River realizes that Vastra misunderstood, but before she can explain Jenny gasps and tells Vastra that she forgot to lock the outside door. She begs her wife for forgiveness and says that she's been murdered, and then fades away.
Realizing that they're all in danger, River slaps Vastra and tells her to wake up. The Silurian wakes up in the conference room and discovers that the Whisper Men--eyeless beings in Victorian suits--are standing over Jenny's corpse. River wakes up Strax, who discover that he's also surrounded by the Whisper Men. In the dream of the conference room, Whisper Men surrounded River and Clara, and the face of Dr. Simeon, the representation of the Great Intelligence, appears and tells River that he will kill the Doctor's friends unless he travels to Trenzalore.
Clara wakes up in her room and hears the Doctor downstairs. She checks on him and discovers that he offered to babysit Angie and Artie, and that they've managed to give him the slip and go out to the cinema. When Clara tells him what happened, the Doctor admits that River is an ex and breaks into tears when he learns what occurred. He gets hold of himself and runs out to the TARDIS, and Clara goes after him. As he links Clara to the TARDIS' telepathic circuits so he can set the coordinates she saw, the Doctor explains that he knows about Trenzalore but has never gone there because it's where he dies and is buried in his own future.
The TARDIS departs for Trenzalore but soon tries to go off-course when it realizes that the Doctor plans to cross his future timestream. The Doctor manages to override the controls, blowing out many of the circuits, and the TARDIS grounds to a halt rather than materialize on the planet. The Doctor opens the exterior doors and confirms that they're hovering over Trenzalore. Since the TARDIS won't move, he cuts the anti-grav circuits and they plummet to the planet's surface. They leave and make their way through the cemetery, and the Doctor warns Clara that his grave is potentially the most dangerous place in the universe. Thousands of soldiers are buried in the cemetery, with higher-ranking ones possessing larger tombstones. They come to the largest tombstone of all: the Doctor's, towering dozens of stories over all the rest. He tells Clara that it actually is the TARDIS, which upon dying leaked out some of its internal dimensions, growing it to giant
As the Doctor approaches his grave, River appears. The Doctor ignores her and River tells Clara that only she can see her because they're still linked via the dream. As he looks round, the Doctor says River's name... because it's on a tombstone. He says that River has been dead for a long time and is insistent that she can't have a tombstone on Trenzalore. A group of Whisper Men approach them, muttering, and prove resistant to the sonic screwdriver. River tells Clara that the gravestone is an entrance to the tomb and when Clara echoes her comments out loud, the Doctor agrees and activates it. As they plummet through a trapdoor, the Doctor casually mentions that River is his wife.
Vastra and Strax wake up inside the tomb and find Jenny's corpse nearby. Strax discovers that she died of shock and is able to reactivate her heart. The Great Intelligence, occupying a duplicate of Simeon's body, arrives with his Whisper Men and welcomes them to the tomb of a being that slaughtered ten billion individuals: the Doctor.
The Doctor and Clara enter the catacombs beneath the grave, with River accompanying Clara. She tells Clara that when she died, the Doctor saved her in a database within a great library, and she now exists as a data ghost. Whisper Men approach them and the Doctor grabs Clara and runs.
Simeon tells Vastra, Jenny, and Strax that the Doctor was killed in a "minor" skirmish on Trenzalore and was buried with the millions of soldiers who died along with him. Vastra insists that the Doctor isn't a tyrant, but Simeon describes of the genocide that the Doctor has committed across time and space. When the Silurian wonders how Simeon knows so much of the Doctor's history, the Great Intelligence removes its current body, leaving it to collapse, an empty bag of skin. The being enters the body of one of the Whisper Men, transforming it to match Simeon's appearance, and explains that it is an incorporeal consciousness made up of sheer data.
The Doctor and Clara manage to stay ahead of the Whisper Men, and Clara starts to collapse from the TARDIS energies. The Doctor warns her that the energies are dangerous, and Clara remembers experiencing something similar in another TARDIS wreck. She starts to remember what happened in the alternate timeline, the one that never happened after the Doctor prevented the TARDIS' crashing. The Doctor realizes that leakage from the TARDIS' telepathic circuits are causing her to remember, and Clara says that she has met him many times throughout time and space, and died before his eyes. As the Whisper Men close in, the Doctor grabs Clara and keeps running.
Simeon explains that to enter the central vault of the grave, they must open the door. It is keyed to a specific password: the Doctor's voice. The Doctor arrives and Simeon orders him to speak his name and open the door. When the Time Lord refuses, the Whisper Men advance on the others. Strax tries to fend them off but they're incorporeal. Each Whisper Man drives his hand into the chest of one of the prisoners, reaching for their hearts. Despite his friend's agony, the Doctor refuses to cooperate.
The vault door opens as the Doctor stares in surprise, and Simeon orders his Whisper Men to stop. River tells Clara that she told the doors the Doctor's name, causing them to open. The Doctor confirms that Clara is okay and then asks Simeon what he plans to do. Simeon promises the Doctor everlasting pain and has the Doctor lead them inside. The console room is long deserted and overgrown, and the cloister bell rings in the distance. In the center of the console room where the console itself once stood is a glowing web of light. The Doctor tells the others that his body isn't there, and that the web of light is the scar tissue of his lifetime of journeys through time and space, from Gallifrey to Trenzalore.
Confronted with all of his existence at once, the paradox overwhelms the Doctor, who collapses to the ground. Simeon approaches the light and explains that he plans to enter the Doctor's timeline and then go back and undo everything good that he has ever done. The process will tear him apart, scattering him throughout the Doctor's history, but Simeon doesn't care as long as he gets his revenge. He enters the rift and disappears, and the Whisper Men vanish as well. History changes as the Doctor moves back through time, killing the Doctor in every incarnation. In the present, the Doctor screams in agony as he dies in the past, one life at a time.
Vastra, monitoring the time rift, traces Simeon's path through history. When she mentions the Dalek Asylum, Clara has vague memories of having saved the Doctor there. Vastra and her friends go outside and discover that the stars in the sky are winking out one by one as all of the worlds and galaxies that the Doctor saved disappear as their pasts are undone. Jenny disappears, since the Doctor saving her and introducing her to Vastra never occurs. Strax's past also changes and he tries to kill Vastra as a genetically impure creature. She has no choice but to shoot and kill her friend before he kills her.
Inside the vault, Clara realizes that she has to enter the time rift and undo what Simeon has done. The Doctor begs her not to, but Clara tells him that what she will do has already happened in the past, and she has no choice but to do it again and fulfill her destiny. River appears and warns Clara that she will be torn apart when she enters the rift, even though millions of copies of her will be created as she splinters through time. Clara is satisfied that she will live on through her copies, even when she dies. Vastra comes back in and Clara tells her to leave and remember her.
Kneeling by the Doctor, Clara tells him that if he sees her again, to run. She calls him "clever boy" and asks him to remember her, and then jumps into the rift. As River said, Clara is copied millions of times and ends up in the Doctor's past. Her copies find all of Simeon's alterations and undo them, restoring history to what it was.
Back at the vault, Strax and Jenny return, back to their old selves. The Doctor, restored, tells the others that he plans to enter the rift and rescue Clara. He instructs them to go to the TARDIS and use the fast-return switch to get back to home. River, still invisible, angrily tells the Doctor not to be stupid and slaps him... and he grabs her hand. When she wonders how he can do that, the Doctor explains that he could see and hear her all along. He didn't reveal his knowledge but it hurt too much.
The Doctor kisses her and wonders why she hasn't faded away, and River says that it's hard to leave for good when she hasn't said goodbye to her. He admits that he doesn't know how to say goodbye, and River tells him to do it like he plans to return to her. After a moment, the Doctor smiles and says that he'll see her around. Before he can, go, River says that there's one more thing. She points out that she was reliant on her mental link with Clara to exist, but Clara is dead. When the doctor wonders how she did it, River simply says "Spoilers" and disappears. The Doctor turns and braces himself, and then steps into his history.
Clara lands in a vast barren landscape, filled with fog and running figures of the Doctor in all his incarnations. The Doctor calls to her, explaining that the landscape is all of his days, good and bad, but his timeline is collapsing. However, he refuses to escape until he knows Clara is safe. She can't get to him, and the Doctor sends her something from her past: the leaf that brought her mother and father together, the leaf that in a sense created her. The Doctor tells her to hold onto it and let it take her home. As Clara clutches the leaf in her hand, the Doctor appears and says that she can leave with him because she's his impossible girl. He asks her to let him save her for once, and Clara runs to him and they hug.
As they prepare to leave, the Doctor and Clara see a figure standing nearby, his back turned toward them. Clara says that she saw all of the Doctor's faces and the figure wasn't there, but the Doctor points out that he is the only one there. However, he explains that while the newcomer is him, it's not the Doctor. "The Doctor" is a name that he chose, a promise that he made... and the figure broke that promise. That is the Doctor's secret. Clara passes out from the strain and the Doctor picks her up and prepares to leave. The figure calls him to, insisting that he had no choice but to do bring peace and sanity. The Doctor agrees, but says that he didn't do it in the name of the Doctor.