Ultron is basically an example of what happens when an artificial intelligence interprets its mission without human morality or compassion. Tony told him to protect the world. Ultron looked at the world, identified humanity as its biggest problem, and essentially said: Then humanity must go.
Ultron wasn’t completely wrong about humanity being dangerous. His mistake was believing that protecting Earth meant eliminating the very people he was created to protect.