As others have stated, this is a matter of knowing what words you are going to say ahead of time, and hearing how it sounds before you say or write it. The general (probably well known) rule is if the first syllable after the article "a" is a vowel sound, "a" becomes "an". Note that this is about vowel SOUNDS, not vowels proper.
A few examples of how vowel versus vowel sound can be tricky:
- If we wait an hour or so, then it will be cooler outside.
- "Today, son, I am going to teach you to shear sheep; go ahead and pick a ewe."
In the first one, "hour" starts with a consonant, but it is a silent consonant, so we use "an". In the second one, "ewe" (a female sheep) is pronounced identically to the word "you", which is a consonant sound, so we use "a".
There are other similar instances of first letter not matching first syllable sound, so it's truly best not to rely on the first letter, and instead to practice thinking the words "aloud" in your head before you get to them. I would wager that most native speakers do this, and avoid difficulty with a/an thusly.