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 work. His wife tells him that everything in the world is second to his job including her. The two discover they are not alone; a young man (Donnie Wahlberg) appears brandishing a gun. He says that he doesn't want to be afraid anymore and accuses Crowe of failing him. Crowe recognizes him as Vincent Grey, a former patient whom he treated as a child for hallucinations. Grey shoots Crowe in the lower abdomen, and seconds later turns the gun on himself.
The next fall, Crowe begins working with another boy, nine year-old Cole Sear (Haley Joel Osment), who has 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, his relationship with his wife begins to fall apart due to his focus on work.
Once Crowe earns his trust, 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. 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. One is an overworked wife abused by her husband who slit her wrists; another that tries to hurt Cole is only heard as a voice who starts calmly pleading to Cole to let him out of the dark cupboard because he's suffocating, then yells at Cole that he didn't steal "the Master's horse" and threatens to attack Cole if he doesn't obey. The third ghost appears very briefly; a boy a bit older than Cole asks him to come into the room, to find the boy's father's gun. The boy turns around to show that he has a large gunshot exit wound on the back of his head.
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 died after a prolonged illness and funeral guests note that Kyra's younger sister is starting to get sick, too. Kyra's ghost appears and gives Cole a box, which is opened to reveal a videotape. When Cole gives it to Kyra's father, the videotape shows Kyra's mother putting floor cleaner fluid in Kyra's food while she cared for Kyra during her illness. The continual illness may point to a slow poisoning in a case of Munchausen syndrome by proxy.
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 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.
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 actually killed by Vincent and was unknowingly dead the entire time he was working with Cole. 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.