A vowel is a "syllabic speech sound pronounced without any stricture in the vocal tract" (Wikipedia). Although the word "university" starts with a "u," the sound is the same as the sound in "you." Say that word out loud and note that you have to use your tongue to make it. It isn't a vowel sound, so we would say "a university."
Contrast this with the word "ugly" which starts with the sound "uhhhh." You don't need to use your tongue or teeth or any other "stricture" to make this sound. It is a vowel, and we would say "an ugly duckling."
As you said, the choice of "a" or "an" is all about the initial sound, not the initial letter, and in English, some letters can sound like vowels or consonants depending on the word.
Why do we use "an" before vowel sounds? Because English speakers don't like glottal stops, where you cut off the flow of air at the throat, as in "uh-oh." Glottal stops are only used rarely in English and they interrupt the smooth flow of a spoken sentence. To say "a ugly duckling," you would have to make a glottal stop to prevent the "a" sound from blending in with the "u" ("uhh") sound. To avoid this, we insert the "n" consonant to separate the vowels without a glottal stop: "an ugly duckling." It just sounds smoother to an Anglophone's ear. Understanding this may help you decide when to use "an" or "a."