These are called Airport Identification Codes. The same ID code might exist in different countries, so it's not really possible to limit your 17,576 total combinations to all airports in the world. Additionally, some countries will use a combination of letters and numbers.
There are three main conventions for determining an Airport ID code:
- Each country has their own national authority, which assigns IDs to airports in that country
- ICAO - The ICAO organization has a process for each country to assign an ICAO ID to airports within that country.
- IATA - The IATA organization has a process for each country to assign an IATA ID to airprots within that country.
National Authority:
Within each country, the national aviation authority assigns Airport IDs to all airports within its jurisdiction. For example, in the USA, the FAA assigns either 3- or 4-letter codes to its airports. There is more information on FAA-specific codes at this ASE page. The busier airports in the USA will get a 3-letter ID. These IDs will usually match the IATA code. And, within the 48-contiguous states, the 3-letter ID will be prefaced with the ICAO-designated letter "K".
International Civil Aviation Organization (ICAO).
This body assigns a prefix designator letter to geographical regions and (if necessary) a second letter to individual countries within that region. Some examples: Northern European airports all start with E, and then the UK starts with G and Germany starts with D. From there, the national authority selects the final two letters. Thus, London Heathrow's ICAO Code is: EGLL.
International Air Transport Association (IATA)
IATA is not driven by the local authority, but rather by the Association itself, in cooperation with the member airlines. IATA assigns a 3-letter code to airports which receive service by IATA-member airlines.
And to get even more complicated, the same airport might have three different ID Codes, one of each.
Related reading:
Can two airports have a different ICAO code but share the same IATA code?
When do we use IATA codes and when do we use ICAO codes?