There are several small hoops, pitfalls, and gotchas that you need to be careful with, but the big requirements are-
1. Your tax home must be in a foreign country, and your abode can be anywhere except the United States. These terms are different, the definitions are discussed later.
2. You must have foreign earned income. More on the definition of earned income later (see What is considered foreign earned income?)
3. You must satisfy one of the following (a b or c)-
a. United States citizen who is a bona fide resident of a foreign country or countries for an uninterrupted period that includes an entire tax year. Or,
b. United States citizen or resident alien who is physically present in a foreign country or countries for at least 330 full days during any period of 12 consecutive months. Or,
c. United States resident alien who is a citizen or national of a country with which the United States has an income tax treaty in effect and who is a bona fide resident of a foreign country or countries for an uninterrupted period that includes an entire tax year.
You must satisfy all three parts above (1, 2 and 3). If you are married to another foreign income earner, you do not have to meet the same test of a b or c above. You could be a bona fide resident (a) while your spouse qualifies as physically present (b).
Some people are simply not eligible for the exclusion of foreign income. For example, US government employees paid by the US government cannot claim the exclusion. There are a handful of other ineligible taxpayers based on their association with the US government, armed services and the like.