Well, there does tend to be a correlation between quality and ratings...
And ratings aren't always the only concern. Sometimes, there is something more being considered. Many kids' shows, for example, often get cancelled due to poor merchandise sales, even if the show, itself, has great ratings.