Only look at the word immediately following the article
Whether you use a and an for the indefinite article is always determined by the first sound (NOT the first letter!) of the very next word.
The grammatical role of that word in the noun phrase is irrelevant. Spelling is also completely irrelevant, except insofar as it relates to the pronunciation. Since English spelling often does not relate to pronunciation in a straightforward way, spelling is an unreliable guide to the use of a vs. an.
If you’re unsure of the pronunciation of a word or phrase, you can look it up in a dictionary that has a pronunciation guide. There are many good online dictionaries available for free; you can see a list of some of them at the following post: What good reference works on English are available?
It's based on pronunciation: a before consonant sounds, an before vowel sounds
The rule is simple, as long as you keep in mind the very important fact that it is based purely on pronunciation and never based on spelling. In modern standard English, we use a before words that start with consonant sounds when they are transcribed phonemically, and an before words that start with vowel sounds when they are transcribed phonemically.
What is a consonant and what is a vowel?
As I said already, this has nothing to do with ordinary English spelling. You need to use your ears, or look at a phonemic transcription of the word (most of these are based on the International Phonetic Alphabet).
Some of the letters used in the transcription many be unfamiliar to you. If so, it may be helpful to view the lists of IPA letters and example words in the following lecture notes: The Vowels and Consonants of English, by Nigel Musk. If you've chosen to use a dictionary that uses a different system from the IPA, then you should look to see if it has a table of the sounds used in its transcription system where they are classified as consonants and vowels.
It's generally fairly intuitive, but keep in mind that the semivowel sounds /w/ (the “w” sound at the start of wing) and /j/ (the “y” sound at the start of year) count as consonant sounds for the purpose of this rule, despite their phonetic similarity to vowels. So we say "a wing" and "a year."
Example words, some with misleading spellings
Here are some examples:
- “an apple,” because apple is the next word after the article, it is pronounced /æpl̩/, and /æ/ is a vowel sound
- “a green apple,” because green is the next word after the article, it is pronounced /ɡriːn/, and /ɡ/ is a consonant sound
- “a Pop-Tart,” because Pop-Tart is the next word after the article, it is pronounced ˈpɑpˌtɑrt/, and /p/ is a consonant sound
- “an enormous Pop-Tart,” because enormous is the next word after the article, it is pronounced /ɪˈnɔrməs/, and /ɪ/ is a vowel sound
- “an hour,” because hour is the next word after the article, it is pronounced /aʊɚ/, and /aʊ/ is a vowel sound
- “a user,” because user is the next word after the article, it is pronounced /juːzɚ/, and /j/ is a consonant sound (for more examples like this, see Is it "a uniform" or "an uniform"?)
- "a European country," because European is the next word after the article, it is pronounced /ˌjʊrəˈpiːən/, and /j/ is a consonant sound
- “an orange T-shirt,” because orange is the next word after the article, it is pronounced /ɔrəndʒ/, and /ɔ/ is a vowel sound
- “a one-time offer,” because one is the next word after the article, it is pronounced /wʌn/, and /w/ is a consonant sound (for more examples like this, see Indefinite article confusion preceding "one-to-one")
- “an 1800s town,” because 1800s is the next word after the article, it is pronounced /eɪtiːnhʌndrədz/, and /eɪ/ is a vowel sound
- “an FAQ,” because FAQ is the next word after the article, it can be pronounced /ˌɛfˌeɪˈkjuː/, and /ɛ/ is a vowel sound
- “a FAQ,” because FAQ is the next word after the article, it can be pronounced /fæk/, and /f/ is a consonant sound (see the following question for more information about the pronunciation of "FAQ": What is the commonly accepted pronunciation of FAQ?)
- "an S curve" (or "an S-curve") because "S" is the next word after the article, it is pronounced /ɛs/, and /ɛ/ is a vowel sound (for more examples like this, see Does one use 'a' or 'an' before the word X-Ray?)
“An historic”: optional exceptions to the main rule
It is always acceptable to follow the rule described above and use a before words that start in pronunciation with the sound /h/, which is considered a consonant sound in English. (As always, be mindful of the distinction between spelling and pronunciation: some words spelled with "H", such as hour or honest, are never pronounced with /h/, and so we always use an in phrases like "an hour" or "an honest effort", because the words hour and honest start with vowel sounds.)
However, there is an optional exception. A number of style guides describe it as acceptable—but not mandatory—to use an before words that start with the consonant sound /h/ followed by an unstressed syllable, such as historical (stressed on the second syllable: "hi-STOR-ical"). That is, both "a historical" and "an historical" can be considered correct (although specific individuals may have more restrictive judgements). In contrast, my understanding is that words that start with the sound /h/ in a stressed syllable, such as history (stressed "HIS-tory"), are always used with the article a instead of an in standard present-day English. So only a can be used in "a history" (unless you pronounce the word without an /h/ sound). (It is possible to find historical examples of "an history"; for more on this, see below.)
The use of "an" before words like historical may have originally been an effect of speakers dropping the "h" sound before an unstressed vowel, but modern English speakers who use "an historical" and like phrases don't necessarily drop the /h/ sound entirely.
For more on this, see the answers to Why we say "an historical" but "a history" and When should I use "a" versus "an" in front of a word beginning with the letter h? (for the latter question, look at all the answers, not just the top ones, to get a complete picture).
See also the following Word Reference thread: A/an: historic, historian, historical, hotel, humanitarian, Hawaiian, honour, herb, hypothesis ...
"A apple": possible for some speakers, but not standard
Sometimes, you might hear people use "a" before a word that starts with a vowel sound, with a glottal stop in between the two vowel sounds. For example, someone might say "a apple" (/əˈʔæpl̩/). However, this is not standard, and is only common for some speakers. For other speakers, it might only occur as an occasional disfluency. In writing, the use of "a" before a word that starts with a vowel sound may reflect this phenomenon, but is often just an error resulting from edits that change word order or word choice. In either case, writing a before a word that starts with a vowel sound is non-standard.
Historical usage of “a” and “an” may be different
In earlier stages of English, these articles were not always distributed in the same way as they are now. This is partly because people used to pronounce some words with different sounds. Be aware of this if you're reading an old text, such as the King James Bible.
See the following questions for more information:
Another good resource on this topic is the following blog post by waiwai93: Articles: “A” vs. “An”