"a" is used before a word with a consonant sound in the beginning and "an" is used in an instance where a word begins with a vowel sound. Don't let any British people tell you otherwise: the change doesn't apply to whether it starts with a vowel or not, it applies if it starts with a vowel sound or not.
Vowel sounds are made by a,e,i,o,and u, though not always.
A dog, a horse, a tree, a universe, a house, a carpet
an apple, an agent, an operation, an idiot, an eagle