I agree with BrenBarn, but perhaps it will be useful to express things slightly differently.
To understand the purpose of letters of recommendation, it's important to think about the context. In my experience, the default assessment for a graduate school application is "not enough information" or "insufficiently compelling case." Most of the time, a rejection doesn't mean the committee felt there was enough evidence to prove the applicant was unworthy (although it can mean that for particularly bad applications). Instead, there just wasn't enough to justify admitting this applicant rather than the competition.
For example, grades are not very useful. The ceiling is low, the standards are inconsistent, and in any case getting good grades is a quite different skill from doing good research. Undergraduate research can be a more useful indicator, but it's still pretty limited. Some students have much better access to high-quality research opportunities than others do, so it's hardly a fair comparison. Plus many undergraduate research papers consist of straightforward work on specialized problems, done with considerable guidance and under some time pressure. That's a little closer to professional research than classwork is, but still not so close.
So the basic setting is that admissions committees are desperate for information. Judging research potential is really difficult, and it's at best loosely correlated with most of the hard data in graduate school applications. This is the context for letters of recommendation. If you have interacted closely with the applicant on a substantial undertaking in this field over a period of months or years, then you are in an excellent position to judge their suitability for graduate school. If you can convey this information to the admissions committee in a trustworthy and reliable way, then it can be far more valuable than anything else in the application.
Of course not all letters are useful. A letter saying "Joe got an A in my course" reveals nothing beyond what the committee could have learned from the transcript. More depressingly, some letter writers say substantive things but are not in a position to do so compellingly. If you are completely unknown to the committee, with no reputation or track record of prior students, then your letter will carry less weight (and even less if you don't at least have the excuse of being young).
This isn't as much of a problem as you might guess. Many people in the field have a reputation, even if they have never met anyone on the admissions committee, and they have an incentive not to hurt that reputation by writing foolish or biased letters. If necessary, someone on the committee can get in touch with them to ask further questions. Plus there are all sorts of opportunities for consistency checks (for example, if someone repeatedly says each year's top student is the best in years, that will be noticed).
However, there's still a genuine problem. A small fraction of applicants just aren't in a position to get compelling letters of recommendation, no matter how talented they are. They are going to be rejected through no fault of their own.
That's a sad outcome, but it seems to be unavoidable. If we had a more reliable way to judge research potential, we would eagerly use it. The point of letters isn't that they always help with the decision, but rather that they often help. When they don't help, the application joins the pile of rejections due to lack of information.
For comparison, one of the comments reads:
Now, ideal case, if A=B in ability, two possibilities arise - (1) If recommendation letter matters, then A gets picked over B (which is not fair),
Indeed, it's not fair, but it's impossible to gather enough information to make fair and reliable judgments in every case. Ultimately, the admissions committee has to accept that some wonderful applicants will be rejected because they couldn't prove how wonderful they were. (Not using letters of recommendation would reduce this type of unfairness, but at the cost of greatly reducing the information available to make good decisions in the other cases. It would amount to partially randomizing the decisions, which would not be in the department's best interests.)
To put it rather starkly, fairness is not the admissions committee's primary goal. Instead, the primary goal is to admit as strong an incoming class as possible. Letters of recommendation greatly help on average in achieving that goal, at the cost of disadvantaging certain applicants. This is a price departments are willing to pay.