In the US at least, the rating on a game is determined by a non-government board that consists of industry "experts". It is ENTIRELY optional - no game HAS to have an ESRB rating on it, just like how films don't have to carry a rating as well.
You see, the US has the first amendment, which my nation Australia lacks. This means that the US government CANNOT put a rating on a game or restrict who can buy it and how they can buy it. The policy of not selling violent videogames to minors is entirely optional, but most game stores do it so as not to attract negative publicity.
In Australia, on the other hand, we don't have the first amendment. Instead, we have a government appointed board of idiots who arbitrarily (it seems) decides whether or not something is violent. The decisions made by our censorship board seem almost schizophrenic - I mean, having morphine in Fallout 3 was apparently awful, yet EATING CIVILIANS in Prototype was A-okay.
In any case, ratings in the USA are not government ratings, and they are not mandatory, but they are there to avoid bad publicity. The ratings are actually useful to parents who pay attention to them and aren't morons. In other nations, like mine, the government still has a lingering religious element to it (thanks for nothing UK) and it doesn't feel comfortable unless it's trying to subtly cram its puritanical values down our throat. Granted, the classification board has lost a LOT of power in the past 20 years or so, and virtually no games get banned over here, but every once in a while our classification authorities give a spasmodic reflex and ban a game for no real reason just to remind us that it still exists. I mean, they tried to ban L4D2 due to "graphic" violence, related to the use of melee weapons when applied to zombie skulls, yet they allowed GTAIV in with no problems, even though that game allows you to go base-ball bat crazy on civilians. See what I mean by the fact that their decisions are arbitrary, or even schizophrenic? Zombie brains = bad, but EATING CIVILIANS AND CRUSHING THEIR BODIES WITH A M1A1 Abrams ON THE STREETS OF NEWYORK in Prototype = perfectly fine.
Well whatever - my main point is that ratings can serve a purpose, they are not (in the US) mandatory, and they were initially created to stave off bad publicity.