Common English Grammar Mistakes
The rule is very simple. It depends on the sound at 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 + constant sound
If the following word starts with a consonant sound, then we say a.
An + vowel sound
If the following word starts with a vowel sound, then we say an.
The importance of sound
Normally, we pronounce consonant letters with a consonant sound, and vowel letters with a vowel sound. There are some exceptions. The rule about a or an is still the same. You just need to think about the sound, not the writing.