"In all criminal prosecutions, the accused shall enjoy the right ... to be confronted with the witnesses against him ..."
The Confrontation Clause is part of the Sixth Amendment to the Constitution of the United States of America, which was added to the Constitution in 1791 as part of the Bill of Rights. Like other parts of the Sixth Amendment, it has been a constant subject of appeals to higher courts across the country, and has given rise to an enormous body of case law detailing what it means.
The scope of the clause
Before looking at what the clause actually protects, it's important to note the first four words of the amendment. The Confrontation Clause only applies to "criminal prosecutions." It does not apply to civil lawsuits at all, and there are many types of proceedings that seem somewhat criminal but don't count as criminal prosecutions: things like traffic court, custody hearings, disbarment proceedings, administrative tribunals, parole hearings, deportation hearings, and administrative punishment hearings in the Armed Forces. Even pretrial hearings and grand juries, which generally form part of a criminal prosecution, are not within the scope of the clause: only the actual trial is.
Once you enter that realm, the Confrontation Clause takes effect. It applies to criminal trials in both state and federal courts (state courts are subject to it thanks to incorporation through the Fourteenth Amendment). Even there, the clause is not a blank check for criminal defendants. Their confrontation is still limited by the applicable rules and common law of evidence, and their lawyers are subject to ethics rules when confronting witnesses.
Why the clause was written
In many European legal systems, it was once common for witnesses to testify by ex parte affidavit and never appear in court at the same time as the defendant. There were several high-profile instances of this in the English system, most famously in the treason trial of Sir Walter Raleigh in 1603: Raleigh's alleged accomplice testified against him by affidavit, and despite Raleigh's pleas, the House of Lords refused to bring the accomplice in to be questioned, and ultimately sent Raleigh to the hangman. By the end of the 17th century, courts in England began adopting confrontation rules to prevent such outrageous cases, although the amorphous nature of common law decision making at the time meant that the rules were far from universal. In the American colonies, ex parte affidavits were used well into the 18th century. Then came the American constitution.
In Pennsylvania v. Ritchie (1987), the United States Supreme Court established that the clause has two meanings. As the plain language indicates, it gives criminal defendants, and by extension the judge and jury, the right to physically face anyone who testifies against them. More importantly, it gives them the right to cross examine opposing witnesses.
This is important for a few reasons. Most people have decent built-in bullshit detectors, and many can tell a lie just by its smell. Forcing witnesses to come to court and testify in person gives the factfinder, whether judge or jury, a chance to watch their demeanor and factor it into the believability of their testimony. Confrontation also gives the defendant a chance to impeach the witness: to point out inconsistencies in their story, omitted information that places their story in a different light, or personal background that weighs against their credibility.
The right of cross examination takes priority over the judge and jury's right to see the witness. After all, the amendment is about the accused; letting the factfinder watch the witness speak is an incidental benefit.
Confrontation vs. hearsay
The clause overlaps with the hearsay rule—that a statement made by a declarant out of court cannot be admitted to prove its truth—and also conflicts with many of the exceptions to the rule which allow an available witness to testify to the statements of an unavailable witness. What to do?
In Ohio v. Roberts (1980), the Supreme Court held that an out-of-court statement could only be admitted if the declarant was unavailable, and only then if the statement bore "adequate indicia of reliability," i.e. if it fell under a "firmly rooted" exception to the hearsay rule, or if it carried "particularized guarantees of trustworthiness." This approach tended to give priority to hearsay exceptions, and left the Confrontation Clause very porous in practice.
This approach changed after Justice Antonin Scalia's opinion for a 7-judge majority in Crawford v. Washington (2004), which attempted to reinterpret the rule through its roots in Sir Walter Raleigh. The rule of Crawford is that "testimonial" statements of witnesses out of court may only be admitted when the declarant is unavailable and the defendant has had a prior opportunity to cross-examine. "Testimonial" means that the declarant would reasonably expect their statements to appear in court. Scalia pointed out a number of such statements, including grand jury testimony, affidavits, confessions, and answers to closed-room interrogation by police (but not to friendly questioning by a beat cop on the street).