There does not seem to be a clear rule as to how three letter airport codes are generated.

Originally airport codes were ascribed the same two-letter code as the nearest weather station. In the 1930s as the number of airports grew, they were converted into three characters. Some airports in towns like Los Angeles simply added 'X' to the end of the existing two letter weather code.

Three characters gives 17,576 possible combinations, a number which may soon be insufficient given the global growth of commercial aviation. Already some codes have been recycled (IDE went from New York's former Idlewild airport to Indianola in Mississippi). While most codes are derived from the first three letters (SYD for Sydney, VIE for (Vienna], BOS for Boston), or the most distinguishable letters (HKG for Hong Kong, TLV for Tel Aviv), or are acronyms (SLC for Salt Lake City, PAP for Port au Prince), for a variety of reasons, many airports have codes that seem to bear no resemblance to the names of the cities they serve.

Firstly, most major airports are coded by the airport name, especially since they may be in towns with more than one airport. If you are flying to London you could be heading to LHR (Heathrow), LGW (Gatwick), LCY (London City Airport), LTN (Luton) or STN (Stansted).

In North America, the FAA gave ownership of the first character to some interested parties. The Navy got N, the Canadians got Y and the FCC got W and K to designate to radio stations (K for points west of the Mississippi River and, cleverly, W for those points east). Q is used for international telecommunication points, and the FAA kept Z for its own purposes (ZCX is a computer near Washington DC that coordinates airport traffic). This perhaps sheds light to why we get odd codes like EWR for Newark, ORF for Norfolk and EYW for Key West.

Other differences come from historical reasons - SGN, LED and BOM relate to the renamed cities of Ho Chi Minh City, Saint Petersburg and Mumbai. Some large airports have been built in small localities which in time have long since been forgotten - Chicago's O'Hare International Airport is coded ORD after the quaintly named Orchard Field. In Tennessee, Nashville's main airport is coded BNA in honour of one of its founders, Colonel Harry Berry, and Knoxville airport's TYS code comes from the Tyson family who donated land.

Some airports have been given somewhat undesirable codes:
SEX Sembach Airport, Germany
SUX Sieux Gateway Airport, USA
KKK Kalakaket Airport, USA
SOB Saarmelleek Aerodrome, Hungary
DOA Doany, Madagascar
KIA Kaiapit, Papua New Guinea