If I am in the United States, and I agree to a contract stating that both parties agree to submit to the exclusive jurisdiction of a court in a foreign country, can I be compelled to appear in that foreign court? Wouldn't it be illegal for the clause to exclude the jurisdiction of United States courts?
This is not illegal if the transaction has any relationship to the foreign country (this limitation is called the minimum contacts test). These agreements called choice of law or forum selection clauses are routinely honored.
It usually isn't illegal for a clause to exclude a United States court as a forum as to the parties to the contract.
The Restatement (Second) of the Conflict of Laws § 80 (1971), a source often relied upon by courts regarding common law rules upon which they have no binding precedents, states that:
[T]he parties agreement as to the place of the action cannot oust a state of judicial jurisdiction, but such an agreement will be given effect unless it is unfair or unreasonable.
This doesn't actually divest the U.S. courts of jurisdiction over the case, but does mean that if you bring the case in a U.S. court in circumstances where there is no reason that the clause isn't valid and applicable, that your case will be promptly dismissed, possibly with an award of attorney fees and costs against you for trying to evade a valid contractual provision.
The law concerning the scope of a choice of forum clause's application, and who decides that question, can be somewhat involved, but in particular cases, where the existence of a binding contract is undisputed and no exceptions to the general rule upholding these clauses is present, the enforcement of a choice of forum clause is often uncontroversial. As Wikipedia explains (in the forum selection clause link above):
In Future Industries of America v. Advanced UV Light GmbH, 10-3928, the United States Court of Appeals for the Second Circuit in New York City affirmed the dismissal of a case that sent the parties to Germany because the forum selection clause made German courts the exclusive forum. By contrast, the same court in Global Seafood Inc. v. Bantry Bay Mussels Ltd., 08-1358, affirmed the refusal of the lower court to refer the parties to Ireland because the clause was not exclusive, and the litigation continues in America.
The state of New York has a statute expressly dealing with those circumstances under which a New York court may not dismiss a case on the grounds of forum non conveniens if the parties' contract provides that the agreed upon venue is a court in New York and if the transaction involved an amount more than $1 million.
Currently, a U.S. Circuit Court split is emerging over whether forum selection clauses in a contract supersede pre-existing arbitration clauses in regulatory membership rules, such as FINRA.
Statutory exceptions may apply in some cases.
For example, under U.S. law, a provision allowing a debt collector to bring suit in consumer debt collection cases outside the place where the consumer debtor resides is void as a matter of public policy under the Fair Debt Collection Act.
But, as a general rule, such clauses are not prohibited.
Two of the leading cases upholding such clauses in U.S. law are M/S Bremen v. Zapata Off-Shore Co., 407 U.S. 1, 32 L. Ed. 2d 513, 92 Sup. Ct. 1907 (1972) (discussed in this law review article) and Carnival Cruise Lines, Inc. v. Shute, 499 U.S. 585 (1991). And, although both of these cases were in the specialty of admiralty law there is no reasoning in these cases confining the holding of these cases to a maritime law setting. More law review treatment can be found here.