By first grade, we were taught that the letters a, e, i, o, u, and sometimes y are vowels. Most of us probably accepted what we were told (it was just another "rule" we had to follow), and never questioned why that last part was true (there were other things to wonder about, like snack time). Additionally, we unconditionally accepted that the other 20 letters in the English alphabet were strictly consonants. To most, those vowels and consonants were just letters, but to that first grader who aspired to be a linguist, they were more than merely lines on a page.
Technically, the terms vowel and consonant (from Latin vocalis, meaning "vocal," and Latin consonare, "to sound together") refer to particular speech sounds: a vowel is one made with your mouth open and your tongue in the middle of your mouth not touching your teeth, lips, etc. (in other words, there's minimal manipulation of air flow while expelling a vowel sound); a consonant, on the other hand, is one (such as \p\, \d\, or \s\) that is made by partly or completely stopping the flow of air breathed out from the mouth with the tongue, teeth, lips, etc. What is special about the letter y is that it can represent both kinds of speech sounds—depending on its position and the letters surrounding it in a word.
Y is considered to be a vowel if…
The word has no other vowel: gym, my.
The letter is at the end of a word or syllable: candy, deny, bicycle, acrylic.
The letter is in the middle of a syllable: system, borborygmus.
In such cases, the letter y is pronounced as either the long vowel e or short or long i (usually as a long i when ending a word)—and, for all intents and purposes, it is a vowel. When y forms a diphthong—two vowel sounds joined in one syllable to form one speech sound, such as the "oy" in toy, "ay" in day, and "ey" in monkey—it is also regarded as a vowel.
Typically, y represents a consonant when it starts off a word or syllable, as in yard, lawyer, or beyond. Technically, this sound of \y\ is considered a semivowel or glide, which is a less prominent vowel speech sound that occurs in the articulation of two consecutive vowel sounds unequal in prominence. For example, there's a very brief long e sound when articulating \y\ in yes. Air flow is not impeded in sounding \y\ (if it was y would be a true consonant); however, the mouth is not opened as fully as in articulating the vowel \y\ in early. The result is a vowel-like consonant.
Linguistically, the "sometimes" part of the grade-school lesson doesn't make sense, since the letter y is more commonly pronounced as a vowel. But its consonant sound is unique, and that seems to be why y is more often considered to be a consonant and only "sometimes" a vowel. In other words, we need y more as a consonant in the English language than a vowel.