In the late 21st century, global warming has flooded coastlines, and a drastic reduction of the human population has occurred. There is a new class of robots called Mecha, advanced humanoids capable of emulating thoughts and emotions. David (Osment), a prototype model created by Cybertronics of New Jersey, is designed to resemble a human child and to display love for its human owners. They test their creation with one of their employees, Henry Swinton (Robards), and his wife Monica (O'Connor). The Swintons' son, Martin (Thomas), was placed in suspended animation until a cure can be found for his rare disease which is caused by the Sinclair virus. Although Monica is initially frightened of David, she eventually warms to him and activates his imprinting protocol, which irreversibly causes David to project love for her, the same as any child would love a parent. He is also befriended by Teddy (Angel), a robotic teddy bear, who takes it upon himself to care for David's well-being.
A cure is found for Martin and he is brought home; a sibling rivalry ensues between Martin and David. To get him into trouble, Martin convinces David to go to Monica in the middle of the night and cut off a lock of her hair, but the parents wake up and are very upset. At a pool party, one of Martin's friends activates David's self-protection programming by poking him with a knife. David clings to Martin and they both fall into the pool, where the heavy David sinks to the bottom while still clinging to Martin. Martin is saved from drowning, but Henry in particular is shocked by David's actions, becoming concerned that David's capacity for love has also given him the ability to hate. Henry persuades Monica to return David to Cybertronics, where David will be destroyed. However, Monica cannot bring herself to do this, and instead abandons David in the forest (with Teddy) to hide as an unregistered Mecha. David is captured for an anti-Mecha Flesh Fair, an event where obsolete and unlicensed Mecha are destroyed in front of cheering crowds. David is nearly killed, but the crowd is swayed by his realistic nature and he escapes, along with Gigolo Joe (Law), a male prostitute Mecha on the run after being framed for murder.
The two set out to find the Blue Fairy, whom David remembers from the story The Adventures of Pinocchio. He is convinced that the Blue Fairy will transform him into a human boy, allowing Monica to love him and take him home. Joe and David make their way to Rouge City. Information from a holographic answer engine called "Dr. Know" (Williams) eventually leads them to the top of Rockefeller Center in partially flooded Manhattan. David meets his human creator, Professor Allen Hobby (Hurt), who excitedly tells David that finding him was a test, which has demonstrated the reality of his love and desire. It also becomes clear that many copies of David, along with female versions, are already being manufactured. David sadly realizes he is not unique. A disheartened David attempts to commit suicide by falling from a ledge into the ocean, but Joe rescues him with the amphibicopter. David tells Joe he saw the Blue Fairy underwater, and wants to go down to her. At that moment, Joe is captured by the authorities with the use of an electromagnet, but sets the amphibicopter on submerge. David and Teddy take it to the fairy, which turns out to be a statue from a submerged attraction at Coney Island. Teddy and David become trapped when the Wonder Wheel falls on their vehicle. Believing the Blue Fairy to be real, David asks to be turned into a real boy, repeating his wish without end, until the ocean freezes in another ice age and his internal power source drains away.
Two thousand years later, humans are extinct and Manhattan is buried under several hundred feet of glacial ice. The now highly advanced Mecha have evolved into an intelligent, silicon-based and alien-looking form — with the ability to perform some type of time manipulation and telekinesis. On their project to studying humans — believing it was the key to understanding the meaning of existence — they find David and Teddy and discover they are original Mecha who knew living humans, making the pair very special and unique. David is revived and walks to the frozen Blue Fairy statue, which cracks and collapses as he touches it. Having received and comprehended his memories, the advanced Mecha use these to reconstruct the Swinton home and explain to David via an interactive image of the Blue Fairy (Streep) that it is impossible to make him human. However, at David's insistence, they recreate Monica from DNA in the lock of her hair which Teddy had saved for unknown reasons. One of the futuristic Mecha tells David that the clone can only live for a single day, and the process cannot be repeated. But David keeps insisting, so they fast forward the time to the next morning, and David spends the happiest day of his life with Monica and Teddy. Monica tells David that she loves him, and has always loved him, as she drifts to sleep for the last time. David lies down next to her, closes his eyes and goes "to that place where dreams are born". Teddy enters the room, climbs onto the bed, and watches as David and Monica lie peacefully together.