As the film opens, Dr. Malcolm Crowe (Bruce Willis) a prominent child psychologist, returns home one night with his wife, Anna Crowe (Olivia Williams), from an event in which he was honored for his efforts with children. The two discover they are not alone -- Vincent Grey (Donnie Wahlberg), a former patient of Crowe's, appears in the doorway of their bathroom brandishing a gun saying, "I don't want to be afraid no more." Grey accuses Crowe of failing him, and Crowe recognizes Vincent as a former patient whom he once treated as a child for hallucinations. Grey shoots Crowe in the stomach, and seconds later turns the gun on himself. The scene fades away with Crowe's wife by his side.
The next fall, Crowe is shown working with another boy, nine year-old Cole Sear (Haley Joel Osment), with a condition similar to Vincent's. Crowe becomes dedicated to this patient, though he is haunted by doubts over his ability to help him after his failure with Vincent. Meanwhile, he apparently begins to neglect his wife, with whom his relationship is falling apart.
Crowe earns Cole's trust and Cole eventually confides in him that he "sees dead people that walk around like regular people." Though Crowe at first thinks Cole is delusional, he eventually comes to believe that Cole is telling the truth and that Vincent may have had the same ability as Cole. He realizes this one night as he is listening to one of his old tapes, recorded while he was treating Vincent, and hears the pleading voices of dead people in the background. He suggests to Cole that he try to find a purpose for his gift by communicating with the ghosts, perhaps to aid them in their unfinished business on Earth. Cole at first does not want to, because the ghosts terrify him, but he soon decides to try it.
Cole finally talks to one of the ghosts, a very sick girl who appears in his bedroom. He finds out where the girl -- Kyra Collins (Mischa Barton) -- lived and goes to her house during her funeral reception. Kyra's ghost appears and gives to Cole a box, which is opened to reveal a videotape. When Cole gives it to Kyra's father, the tape reveals that while Kyra was bedridden with illness, she was being poisoned by her mother. Now believing in his ability to use his gift to positive effect, Cole confesses his secret to his mother, Lynn (Toni Collette). Although his mother at first does not believe him, Cole soon tells Lynn that her own mother (Cole's grandmother) once went to see her perform in a dance recital one night when she was a child, and that Lynn was not aware of this because her mother stayed in the back of the audience where she could not be seen. He also tells her the answer to a question she asked when alone at her mother's grave. Lynn tearfully accepts this as the truth. Cole also counsels Crowe on his marriage, suggesting he try talking to his wife while she is asleep.
His faith in himself now restored as a result of his success with Cole, Crowe returns to his home, where he finds his wife asleep on the couch with the couple's wedding video on in the background, not for the first time. As she sleeps, Anna's hand releases Malcolm's wedding ring (which he suddenly discovers he has not been wearing), revealing the twist ending of the film: Crowe himself was unknowingly one of Cole's ghosts, having been killed in the opening scene. Due to Cole's efforts, Crowe's unfinished business -- rectifying his failure to understand Vincent -- is finally complete. Recalling Cole's advice, Crowe speaks to his sleeping wife and fulfills the second reason he returned, saying she was "never second," and that he loves her. Releasing her to move on with her own life, he is free to leave behind the world of the living. The film ends on a short clip of their wedding tape that fades into white.