William Costigan (Leonardo DiCaprio) is a cop working undercover in Frank Costello's (Jack Nicholson) mob. Colin Sullivan (Matt Damon) is a top police detective, who is also Costello's inside informant. Both are dating the same police psychiatrist, Madolyn Murray (Vera Farmiga).
What Sullivan doesn't know is that Costello is also an FBI informant. When he finds out, he arranges for the slaughter of Costello's entire team during a drug deal.
Costello dead, Costigan visits the station where he finds out that Sullivan is the rat in the police dept. Sullivan erases Costigan's identity as a cop, which is only known to two people (Queenan [Martin Sheen], who is dead, and Dignam [Mark Wahlberg], who was put on leave by Sullivan).
Costigan arranges to meet Sullivan and tries to bring him to justice. But Costigan is shot dead by a second Costello rat, as is another officer played by Anthony Anderson. Sullivan then shoots this rat and arranges the crime scene to look like this new rat did it.
But earlier, Costigan gave Madolyn an envelope explaining everything. Sullivan goes home and is shot dead by Dignam, the exiled inside guy, presumably acting on Madolyn's information.
Madolyn lives, carrying Sullivan's baby - or is she...
Mike D adds...
Mandolyn's baby doesn't necessarily belong to Sullivan. It was set up in a few different parts of the movie that Sullivan is "deficient" in bed, and since Costigan and Mandolyn slept together, it seems reasonably clear that we're supposed to believe the baby was Costigan's.