depends on thesoundat the start of the following word. (It does not depend on the way we write the following word, it depends on the way we say it.)
A + consonantsound If the following word starts with aconsonantsound, then we saya.
AN + vowelsound If the following word starts with avowelsound, then we sayan.
The importance of sound
Normally, we pronounce consonant letters with a consonant sound, and vowel letters with a vowel sound. But there are some exceptions. The rule aboutaoranis still the same. You just need to think about the sound, not the writing. Look at these examples: