Andy and the gang got their start when Danny Thomas, in his own show, was arrested by a small town cop for running a stop sign.

The origin for its own show started when Sheldon Leonard, the producer of The Danny Thomas show, hired a writer he was familiar with from the show to write a pilot episode introducing Andy as a police officer in the small town of Mayberry. The idea took flight and Leonard went on to become producer of the show for the entire 8 seasons.

It's hard to imagine the show without Barney, though Don Knotts starred in only 150 of the episodes while Andy starred in each of the 249 total episodes.

The show had popular spin-offs including Gomer Pyle, U.S.M.C. (started at the end of the 4th season) and Mayberry R.F.D.

More Info: en.wikipedia.org