The Great Gazoo was exiled through time and space from the planet Zatox or Zetox to serve and obey whoever found him as punishment -- and he was found by Fred Flintstone and Barney Rubble.
Gazoo was exiled for inventing a tiny button, smaller than a fingernail, that with a single press would have disintegrated everything in the entire universe, although he had no intention of using it -- he was merely showing off that he had the intelligence and powers necessary to create such a thing. Later episodes make it clear that no one in his world actually believed him to be evil in intent but instead considered him dangerously irresponsible and self-indulgent, and he is sent to the time of the Flintstones and Rubbles as much for rehabilitation as for punishment, intended to learn responsibility during his service to Fred and Barney.
In his first episode, the Great Gazoo describes himself to Fred and Barney this way:
"My dear fellow, I'm not only undependable, but I'm a bit of a kook. That's why I'm here, remember: I'm being punished."
"So here I am, forced to servce two prehistoric dum-dums until I prove that I'm reformed."
Also in that first episode, he warns them, "You two are the only ones who can see me." It turns out, however, that small children and animals can also see him, but he is unable to make himself visible to Wilma and Betty even if he is ordered to do so.
Despite an apparent need to be sleeping constantly, the Great Gazoo has been shown to stop time, transform people into other creatures (once turning Wilma and Betty into birds) or alter their sizes and shapes, create robotic flesh-and-blood duplicates of Fred and Barney, and otherwise control reality to whatever extent amused him, and all this while claiming that most of his powers have been taken away from him. Gazoo's powers are implied to be a combination of superscience and psionics (over half his body mass is his cranium), though he laughingly refers to himself one point as a sorcerer. One is left with the impression that in a showdown between Gazoo and the combined forces of Q, Jeannie the djinni, Mr. Mxyzptlk, and Galactus, the Great Gazoo would be the last person left standing -- if he managed to keep from falling asleep during battle.
The character was brilliantly voiced by Harvey Korman, who has stated in interviews his surprise that people still praise him for playing a character that appeared in only half the episodes of the final season of the original Flintstones series and has appeared in none of the animated sequel series.