Only look at the pronunciation of the word immediately following the article
The choice between a and an for the indefinite article is always based on how the beginning of the very next word in the noun phrase is pronounced.
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 there are a lot of cases where English spelling does not relate to the 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?
The main rule in modern standard English: a before consonant sounds, an before vowel sounds
Fortunately, there is a very simple rule that you can follow that will never lead you astray, 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, both written and spoken, 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've already said, 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 /griːn/, and /g/ 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 doubt 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 class of exceptions to the main rule
As I said earlier, following the main standard rule is always acceptable. However, there is a set of words where it has often been considered acceptable, but optional, to follow a different rule.
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") the article an may be used instead of a. That is, both "a historical" and "an historical" are generally considered acceptable (although specific individuals may have more restrictive judgements). The use of "an" in words like this may have originally been an effect of speakers dropping the "h" in this position, 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).
But 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.)
Words that are spelled with the letter "h", but pronounced starting with a vowel sound are always used with the article "an", in accordance with the main rule covered earlier in this answer. So only "an" can be used in "an hour" and "an honest effort".
See also the following Word Reference thread: A/an: historic, historian, historical, hotel, humanitarian, Hawaiian, honour, herb, hypothesis ...
"A apple": possible for some people in spontaneous speech, 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”