There are many reasons behind the codes and one could write a long article about them (there's a link to one above, but it's got a few inaccuracies).
The codes we use here are the simpler, "public-facing" set determined by IATA, 2 character for airlines and 3-character for airports. Inside the industry, such as on flight plans, there is another set known as ICAO codes, 3-character for airlines and 4-character for airports. The relationship betwen these two sets gives some more clues to the more puzzling codes, such as why all the Canadian ones begin with Y.
Two-character airline codes are in particularly short supply nowadays, which is why new entrants like Jet Blue have to put up with B6 (JB already belonged to helicopter operator Helijet in Vancouver BC). Jet Blue's ICAO code is more sensibly JBU. There has long been a proposal to make the 3-character airline codes standard, but as the established airlines would have to reprogram all their computer systems and it would only really benefit the new entrants, this has been resisted.