Each side paying for their own litigation costs is called the American Rule. As you noted in your question, it contrasts the English Rule where the losing party pays the winning party's litigation costs.
In the United States, there are literally thousands of specific exceptions to the American Rule but they can be divided into these general categories:
Contracts that say the losing party pays. This is one you pointed out in your question and is pretty common.
Common Fund Doctrine. This is legal principle that courts have applied where it would be unfair for a plaintiff to pay their legal fees because it would be ultimately coming out of their pocket. Some classic examples are: A beneficiary suing a trustee for violating his fiduciary duties; shareholders suing the management of a company; and some types of class-action and antitrust cases where the efforts of the attorneys benefited the "common good".
Contempt proceedings. See Toledo Scale Co. v. Computing Scale Co., 261 U.S. 399 (1923). This would only apply where one party is asking the Court to hold an opposing party in contempt, not where a judge initiates a contempt proceeding. Today, most states and the federal system have court rules that would likely apply here as well.
Bad Faith litigation. As you noted in your question. This would be bad-faith/frivolous lawsuits and action in litigation that needlessly delay or increase the expenses of the opposing party.
Statutes. This is by far the largest category. There are too many statutes to list but here is a sample of some broad categories:
- Civil Rights Cases (Civil Rights Act, Voting Rights Act, housing discrimination, Americans with Disabilities Act, Etc.)
- Consumer Protection cases (Fair Credit Reporting Act, Fair Debt Collection Act, etc.)
- Landlord-Tenant cases
- Environmental Protection Cases
- Open Records Law cases (Freedom of Information Act and similar state statutes)
The statutes that contain fee-shifting provisions are generally ones where litigation is thought to be in the "public interest." The idea is that the legislature wants to encourage the private enforcement of certain laws. Allowing the recovery of attorney's fees provides financial incentives for lawyer to take cases where the ultimate damages award is small and might be less than their legal fees.
There is a good law review article in the American University Law Review called The American Rule on Attorney Fee Allocation: The Injured Person's Access to Justice on this topic. While it's 20 years old, the policy considerations and historical perspective remains accurate.