The distinction between these two usages has to do with sound, not just with letters.
The article a is used before words, acronyms or abbreviations beginning with a silent vowel sound or a consonant sound, including /w/ and /y/ sounds (such as the long /u/): a word, a year, a useful invention. The article an is used before words beginning with a silent consonant or a vowel sound: an uncle, an apple, an epidemic, an offer, an income, an eerie movie.
Linguists tell us that an is the older form and that in Middle English it occurred before all nouns. Yet today, children typically use the simple a before nouns (e.g. a apple). Eventually, we correct them, or they figure it out themselves by hearing the correct usage.
With acronyms and abbreviations
Use the same guidelines as above and determine whether the letters are pronounced as a word (NASA, thus a Nasa experiment) or as individual letters (NFL, thus an NFL player). Sometimes letters used as abbreviations may imply a word that our minds read as a word rather than as letters. If that is the case, use the appropriate article depending on how your reader will see and mentally hear it. For example, would your reader say SQL as see-quel or as s-q-l?
On the subject of a/an before h
At one time in the history of English, all forms of the word “history” were pronounced with silent “h.” They aren’t now, but some people continue to pronounce them that way, particularly “historic(al).” Here are some sources discussing usage preferences (and that’s what it is—not a rule):
Evans and Evans:
The form _a_ should be used before an _h_ that is pronounced, as in _history_ and _hotel_. Formerly these _h_ sounds were not pronounced and _an historical novel_, _an hotel_ were as natural as _an honorable man_, _an hour_, _an heiress_. This is no longer true and these archaic _an’s_, familiar from English literature, should not be repeated in modern writing.
Evans, Bergen and Cornelia Evans. A Dictionary of Contemporary American Usage. New York: Random House, 2000.
There is a lingering tendency on the part of some American writers to use ‘an historic document,’ though they wouldn’t be caught even in a British pub saying ‘an hotel.’ But the preferred form these days, on both sides of the Atlantic, is ‘a historic document.’ [I don’t know who prefers it, but maybe B. has done a study.—grammarNOW!]
Bernstein, Theodore. Miss Thistlebottom’s Hobgoblins: The Careful Writer’s Guide to the Taboos, Bugbears, and Outmoded Rules of English Usage. Centro Books, 2006.
———. The Careful Writer. Free Press, 1995.
_A_ is used before all consonants except silent h (_a history, an hour); _an_ was formerly usual before an unaccented syllable beginning with h and is still often seen and heard (_an historian, an hotel, an hysterical scene, an hereditary title, an habitual offender_). But now that the h in such words is pronounced, the distinction has become anomalous and will no doubt disappear in time. Meantime, speakers who like to say _an_ should not try to have it both ways by aspirating the h.
Burchfield, R. W. Fowler’s Modern English Usage. New York: Oxford University Press, 2004.
Chicago Manual of Style 15 (7.46):
The indefinite article a, not an, is used in American English before words beginning with a pronounced h. See also 5.73.
a hotel, a historical study
_but_ an honor, an heir
University of Chicago Press Staff. The Chicago Manual of Style. University of Chicago Press, 2010.
Although I realize I’m in the minority, it’s just awkward for me to say “a historic moment.” My first grammar commandment: Know Thy Audience!