In 1970s South Boston, young Colin Sullivan is introduced to crime by Irish-American mob boss Frank Costello. By 2006, Costello has groomed him as a mole inside the Massachusetts State Police (MSP). Sullivan is accepted into its Special Investigations Unit, which targets organized crime. Before graduating from the MSP’s academy, William “Billy” Costigan Jr. is recruited by Captain Queenan and Staff Sergeant Dignam to go undercover, as his family’s ties to organized crime make him a perfect infiltrator. His cover story is that he is an MSP academy dropout who served time in prison on an assault charge; he subsequently joins Costello’s crew.
Sullivan begins a romance with police psychiatrist Madolyn Madden. Costigan also sees her as a condition of his probation, and they begin a relationship as well. After Costello escapes a sting operation, each mole becomes aware of the other’s existence. Sullivan is promoted to the State Police’s Internal Investigations unit and tasked by Costello to find the rat. Costigan follows Costello into a porn theater, where Costello gives Sullivan an envelope containing personal information on his crew members. Costigan chases Sullivan through Chinatown, but neither man sees the other’s face.
Sullivan later has Queenan tailed to a meeting with Costigan on the roof of an empty building. Costello’s men arrive, and Queenan makes Costigan leave to preserve his cover before confronting them alone. They drop Queenan to his death before engaging in a brief shootout with police. Crew member Delahunt subsequently dies from a gunshot wound, after telling Costigan he knew he was the informant. Television news states that Delahunt had been an undercover cop, but Costello recognizes this as a ploy to protect the real informant. With Queenan dead a furious Dignam is ordered to take a leave of absence after a fight with Sullivan, but he resigns instead.
Using Queenan’s phone, Sullivan reaches Costigan, who refuses to abort his mission. Sullivan learns from Queenan’s diary that Costello is a protected FBI informant, causing him to worry about his own identity being revealed (meanwhile, Costigan also learns Costello is an FBI informant from a bank robber he is sent to brutalise). With Costigan’s help, Costello is traced to a cocaine drop-off, where a gunfight erupts between the crew and the police. Most of the crew are killed. Costello, confronted by Sullivan, admits he is an FBI informant, and Sullivan kills him.
With the operation wrapped up Costigan goes to Sullivan to restore his true identity, but he notices the envelope from Costello on Sullivan’s desk, realizes he’s Costello’s mole and flees. Recognizing Costigan’s revelation, Sullivan erases Costigan’s records from the police computer system. Costigan forwards Madolyn’s recordings, which Costello willed to him, of confidential conversations incriminating Sullivan as a mole, prompting her to abandon Sullivan.
Costigan arranges to meet Sullivan at the building where Queenan died, where he holds him at gunpoint. Trooper Brown, a friend of Costigan’s from the academy, also arrives at his behest, but he has not brought Dignam as requested and instead treats him with suspicion. Trooper Barrigan from Special Investigations arrives and kills both Costigan and Brown, revealing that he was a second mole for Costello. Sullivan then kills Barrigan and frames him as the only mole.
At Costigan’s funeral, Sullivan notices that Madolyn is tearful. She ignores him when he attempts to speak to her. Later, Sullivan arrives home to his apartment and is confronted by Dignam, who kills him. As he leaves, a rat races across the balcony.