Some of the other answers here mention "the book", but it's important to realise that there is no single Asimov book or story on which this movie was based. The plot of the movie is taken partly from a previous script which had no ties to any of Asimov's work; various elements from Asimov's oeuvre were added in later, including the title (from a particular collection of short stories) and the 3 Laws of Robotics (which he used throughout his career).
Asimov first articulated the 3 Laws in a 1942 story called Runaround, and did so explicitly to play around with their limitations. Many of his robot stories use the 3 Laws to examine philosophical questions - when would the Laws be too weak, or too strong, or too open to interpretation?
One of those philosophical conundrums is how a robot imbued with the laws would act given a choice between two courses of action, both of which will lead to humans being harmed, such as only being able to save one of two groups of people. Should the largest number of humans be saved, or should higher value be placed on some than others?
The question of harm to humanity itself was explored a little bit in the story The Evitable Conflict (which was included in the collection I, Robot, as it happens), in which "machines" are key to a kind of global planned economy. They are revealed to be taking political control for the good of humanity, reasoning that they can (and must) both disobey orders, and harm individual humans, if they know it will prevent further harm to humanity as a whole.
The explicit concept of a "Zeroth Law", trumping the First Law, comes from a later novel, Robots and Empire, in which a telepathic robot called R. Giskard Reventlov begins to conceive of it, but is ultimately killed by the conflicts it sets up in his positronic brain. (Asimov always conceived his robots as having brains much more like ours than like a digital computer, with even the 3 Laws as complex potentials in the circuits rather than strictly logical pieces of code.)
So, it is well within Asimov's conception of the laws for a sufficiently advanced robot intelligence to derive a Zeroth Law. Where the movie takes liberties is that such a law would not give the robot in question a complete free reign to harm, since each individual decision must justify breaking the remaining laws; in Asimov's works, this is always a tough decision for the robots to take, rather than something that liberates them to go on a rampage. But that wouldn't have given Will Smith as much chance to shoot stuff and look cool.