When air travel was in its infancy, airports often adopted the two-letter codes that had been assigned to their cities by the National Weather Service. This was much easier than writing out an airport's full name, and it may have begun as a time-saving measure. As the industry expanded, more airports needed codes. In the 1930s, the industry began using three-letter codes, increasing the possible combinations to 17,576.
Many airports just use the original two-letter weather service code followed by an X. This is the case in Los Angeles, LAX, and Portland, PDX. Some codes are easily identified as abbreviations for a city, as in DFW for Dallas Fort Worth. Others refer to some interesting history. For example, the code MSY for New Orleans refers to the Moisant Stock Yards, the sight of a deadly crash early in the history of aviation.
These codes were a simple, standard way to refer to airports, and airports all over the world began using them. Soon, there weren't enough three-letter codes to go around, and the International Civil Aviation Organization began using four-letter codes. In general, the first two letters refer to an airport's country. In the United States, however, the ICAO code is usually just the domestic three-letter code preceded by a K.