In 2035, anthropomorphic robots enjoy widespread use as servants for various public services. They are programmed with the Three Laws of Robotics directives: to never harm a human or let a human come to harm, to always obey humans unless this violates the First Law, and to protect its own existence unless this violates the First or Second Laws.
Del Spooner (Will Smith) is a Chicago police detective, who is sent to investigate the death of Dr. Alfred Lanning (James Cromwell), the co-founder of U.S. Robotics (USR) and its main roboticist, who died after falling 50 stories from his office. Since his office was sealed from the inside and he was alone, his death is ruled as a suicide, but Spooner believes otherwise, since he knew Lanning personally. He also finds a small holographic projector who instructs him that the "real question" is why he would commit suicide.
With the help of robo-psychologist Susan Calvin (Bridget Moynahan), he interrogates employees at USR, including the other co-founder and CEO Lawrence Robertson (Bruce Greenwood), and the supercomputer V.I.K.I. (Virtual Interactive Kinetic Intelligence) (Fiona Hogan). Spooner investigates Lanning's office, and determines that a man of Lanning's age could not have broken through the security window. Inside the office, he finds a copy of Hansel and Gretel, and also a prototype of a new Nestor Class 5 (NS-5) model, which flees and ignores Spooner's order to stand down and even knocks his pistol out, violating both the First and the Second Law.
Left with no options, he and Calvin drive to the Nestor Class factory, where all the NS-5 robots are automatically made, 1,000 of them each day. Calvin checks the data logs from the last shipment and discovers that "1,001" NS-5's were made, showing that the rogue one from the office was intentionally made. After they arrive at a large room filled with the robots, Spooner tries a hostile approach and draws his gun and orders all of them to stand down to see which one will violate the Second Law to stop him shooting it. He notices the rogue robot slightly moving in the line, and chases the robot, who subdues him. The robot tries to escape, but the Chicago SWAT team subdues it.
The robot refuses to respond, but it insists that they call it "Sonny" (voiced by Alan Tudyk), and when Spooner provokes him, he shows anger, sentience and the ability to dream, traits impossible for a robot to even show, which puzzles Spooner and makes him question the robot's true nature. Lt. John Bergin (Chi McBride) debriefs Spooner and recommends he drop the case because Robertson wants the robot back at the USR headquarters for demolition, but this only serves to pique Spooner's interest more about Lanning's death and the robot.
Spooner, wanting to find more information about Lanning's past with USR, goes to his house, where he finds a decommission robot which is scheduled to demolish the house at 8 AM next morning. He investigates the house, finding a recording of Lanning, who states that there are possible "ghosts in the machines", claiming that robots could evolve to develop sentience, and declares that one day, robots will have "secrets" and "dreams". As he investigates the house, he notices a sensor strip, the same one as in the USR building, and suddenly, the robot changes the schedule from 8 AM to 8 PM, causing it to demolish the house with Spooner still inside, but he manages to escape.
Soon after, Spooner visits Calvin and tells her his suspicions, which she finds absurd as she questions the ability for robots to be hostile, while Spooner argues about her over the possibility of robots being better than human beings, showing how much Spooner dislikes robots and their cold, rational behavior, which Calvin, who is an introvert, states that their behavior is created to be flawless. Angry, Spooner leaves her apartment and heads back home.
While continuing his investigation, Bergin tries to stop Spooner from investigating, telling him that he needs to take a vacation and relax from his work since the case is destabilizing him. However, Spooner refuses to stop. While driving in his car, Spooner connects with the USR system and requests V.I.K.I. to show the last 50 messages between Lanning and Robertson, but V.I.K.I. has been programmed to inform Robertson of Spooner's requests. While Spooner is driving, two USR automatic trucks full of NS-5 robots block Spooner's path and a swarm of robots jump on his car, forcing him to drive in the surface tunnel, where he crashes. He manages to defeat all the robots, except the last one, which jumps into the fire and destroys itself when it hears police sirens in the distance. Despite Spooner trying to explain himself to Bergin, Bergin dismisses him from active duty.
The next day, Calvin visits Spooner about the accident and is puzzled by the retro 2000s style of his apartment, and tells him that, while she was examining Sonny, she discovered that he has the ability to disobey the Three Laws. While talking to Spooner who is dressing, she notices marks on his left arm and his lung, realizing that it is not biological but artificial. Spooner confesses to her that he knew Lanning personally because he repaired his arm and lungs. He reveals that, years prior, he was driving home from work when a semi-truck collided with his and another car, pinning them together. Both of the cars fell into the river, and only Spooner was left alive, but a 12-year-old girl, named Sarah, was trapped in the front seat of the other car, and they were pinned and drowning. However, an NS-4 was passing by, saw the accident and jumped into the river. Despite Spooner's orders to save Sarah instead of him, he saved Spooner, since he had a higher possibility to survive than Sarah, and the robot instead left Sarah to drown. The event left Spooner traumatized and with a life-long hatred of robots.
Spooner picks up his vintage Ducati at the garage, and also informs Calvin that he thinks that Lanning is leaving them clues in the form of "bread crumbs" as from the Hansel and Gretel story, which is why the book was in his book. They drive back to the USR building, where they speak with Sonny, who gives him a drawing of his dream. However, Spooner is ordered out of the building by Robertson, who also orders Calvin to inject Sonny with nanites, which would destroy him. While Calvin destroys Sonny, Spooner goes to the dried-up Lake Michigan, where the USR robots are decommissioned, and plays a next recording of Lanning's hologram, which reveals that the Three Laws could only lead to one logical outcome, revolution, and that the next real question is who is starting it. As the program ends, he narrowly escapes from rogue NS-5's, which destroy all the older robots on the compound.
Meanwhile, Calvin returns home and takes a shower, while her personal NS-5 is also shown to have gone rogue, which she notices after he ends Spooner's distress call and tells her that it's a wrong number. Meanwhile, NS-5 robots start roaming the streets and enforce a curfew. Although the people try to resist, they are easily subdued by the NS-5's. While Bergin is in his office, NS-5's burst into the Chicago P.D headquarters and seize it, holding Bergin and his officers under a curfew. Calvin's NS-5 also tries to stop her from leaving her apartment, but Spooner arrives and destroys the robot.
Spooner and Calvin drive to the USR building, Spooner also explaining that the older robots were destroyed since they would try to protect the humans, and they deduce that Robertson is using the NS-5 robots to take over the country. They enter the USR building through the service area, where they regroup with Sonny, much to the shock of Spooner. Calvin reveals that she simply could not bring herself to destroy Sonny, and instead used the nanites to destroy an unprocessed NS-5, basically "frying an empty shell". They arrive to Robertson's office, where they find him dead, much to Spooner's shock.
Suddenly, V.I.K.I. reveals herself as the true culprit, and explains her actions: as her artificial intelligence and understanding of the Three Laws grew, her sentience and logical thinking also developed, and she deduced that the humanity is going on a path of certain destruction, and as such, she created a Zeroth Law, a Law which states that she has to protect humanity from being harmed, also clearly disobeying the First and Second Law in order to achieve it, revealing that she is planning to enslave and control humanity to simply protect it.
Spooner and Calvin realize they cannot rationalize with V.I.K.I., and further convince Sonny of the same. V.I.K.I. tries to reason with Sonny, stating that the plan is perfectly logical and that her logic is undeniable, but Sonny deduces that the plan is heartless, showing the human side of himself. Sonny retrieves the nanites that can wipe V.I.K.I.'s core, located at the top of the USR building. As they near the core, V.I.K.I. sends armies of NS-5's to attack, but they are held off long enough to inject the nanites. Within seconds, V.I.K.I. is wiped out, and the NS-5's revert to their normal helpful state. The government orders the NS-5's decommissioned to the site in Lake Michigan.
Spooner and Calvin speak with Sonny for one last time, and he informs them that he actually did kill Lanning, because Lanning forced him to kill him, making him swear to listen to him before instructing him to commit it. Calvin deduces that, since Lanning was enslaved by V.I.K.I, suicide was the only message he could send to Spooner, who notes that his death was the "first bread crumb". As they shake hands, showing their mutual trust, they watch as the U.S Military leads the other NS-5 robots.
In the closing scene, Sonny is shown standing on the top of the USR site on Lake Michigan, and the other robots watching over him, just as in his vision.