John Watson, whose wife Mary (Amanda Abbington) is now visibly pregnant, finds a dishevelled Sherlock, under the influence of drugs. John attempts to force him to rehabilitate, but Sherlock insists that he was undercover for a case. Mycroft (Mark Gatiss) realises that Sherlock, on behalf of a senior member of the government (Lindsay Duncan), is trying to take on Charles Augustus Magnussen (Lars Mikkelsen), a newspaper owner who blackmails people, and warns him not to. Magnussen visits Sherlock to intimidate him and refuses to enter negotiations over the stolen letters.
John is also baffled to find Sherlock is now in a relationship with Mary's bridesmaid, Janine (Yasmine Akram), until he learns that Janine is Magnussen's personal assistant and the liaison a ruse. With her help, Sherlock breaks into Magnussen's apartment, where he is shocked to find Mary holding Magnussen at gunpoint. Mary shoots Sherlock, who harnesses all his mental powers to stay conscious. He is taken to hospital, where Mary warns him not to denounce her. John nonetheless begins to suspect something.
Sherlock runs away from the hospital and arranges a confrontation with Mary, who confesses to having a past as a secret agent and assassin with which Magnussen is blackmailing her. Sherlock deduces that she intentionally shot him in a non-lethal spot and called an ambulance. She says she is desperate to keep John in the dark as she loves him. John, however, witnesses the conversation and is left in turmoil. Sherlock tells John that he observes John's addiction to danger and that John was most probably attracted to Mary because of her hidden past. Mary gives John a memory stick containing information on her, marked with her true initials A. G. RA., telling him the contents will destroy his love for her. After not viewing the memory stick for a few months, John decides to destroy it without reading it and, to her relief, to love Mary regardless. While the couple spend Christmas with the Holmes brothers at their parents' home, Sherlock takes John to meet Magnussen at Magnussen's property, Appledore, which Holmes believes contains Magnussen's blackmailing archive. During their confrontation, Magnussen explains his purpose in putting pressure on Mary was to arrive at Mycroft via John and Sherlock.
Sherlock offers to buy Magnussen's information on Mary with the state secrets contained in Mycroft's laptop, which he has stolen. Magnussen, however, is aware that Sherlock is setting up a trap: security services looking for the laptop will raid his vaults, find their contents and arrest him as a blackmailer, thereby vindicating Sherlock and John. Magnussen reveals that this will not work because there are no vaults: he actually relies entirely on his memory and keeps no physical records. By giving Magnussen the laptop, Sherlock and John are now guilty of attempting to sell government secrets, whereas Magnussen cannot be charged with anything. When Mycroft and the police arrive, Sherlock shoots Magnussen in the head, realising that he has no alternative if he is to protect the Watsons from Mary's past, and save John from being charged with high treason.
Mycroft convinces the government to spare Sherlock a trial and, as an alternative punishment, press him into a highly dangerous mission in Eastern Europe. He is, however, recalled within minutes when TV screens all over Great Britain are hacked to broadcast a loop of a static image of Jim Moriarty (Andrew Scott) asking "Did you miss me?"