The FAA's rules are in their Location Identifiers paper, which explains how the numbers are used, at least for the US. It also mentions Transport Canada, by the way, and I guess that Canada uses similar rules.
There are specific requirements for an airport to qualify for a three-letter identifier:
a. Three-letter identifiers are assigned as radio call signs to
aeronautical navigation aids; to airports with a manned air traffic
control facility or navigational aid within airport boundary; to
airports that receive scheduled route air carrier or military airlift
service, and to airports designated by the U.S. Customs Service as
Airports of Entry. Some of these identifiers are assigned to certain
staffed aviation weather reporting stations or for airports
commissioning Automated Weather Observation Systems, level III
(AWOS-III) or higher that have paved runways 5,000 ft or longer.
That three-letter code is then prefixed with 'K' for the continental US, to give the four-letter international code:
An international location indicator is a four-letter code used in
international telecommunications. The location indicator for airports
in the contiguous United States is the three-letter identifier
preceded by "K". For other non-contiguous United States airports, the
following two letter prefix will be used:
Alaska - PA, PF, PO, PP
Hawaii - PH
Puerto Rico - TJ
Virgin Islands - TI
(See ICAO Document 7910 for listings.)
The one-letter two-number format is usually used for public airports that don't qualify for a three-letter identifier:
d. Most one-letter, two-number identifiers are assigned to public-use
landing facilities within the United States and its jurisdictions,
which do not meet the requirements for identifiers in the three-letter
series. Some of these identifiers are also assigned to aviation
weather reporting stations.
- One-letter, two-number identifiers are keyed by the alphabetical letter. The letter may appear in the first, middle or last position in
the combination of three characters. When the letter signifies an Air
Traffic Control Center's area, the assignment will not change if the
Center's boundaries are realigned.
The two-letter, two-number format is usually used for private airports that don't qualify for a three-letter identifier:
e. Two-letter, two-number identifiers are assigned to private-use
landing facilities in the United States and its jurisdictions which do
not meet the requirements for three-character assignments. They are
keyed by the two-letter Post Office or supplemental abbreviation
(listed below) of the state with which they are associated. The
two-letter code appears in the first two, middle, or last two
positions of the four-character code.