In Latin, the present infinitive was marked with a suffix -(e)re attached to the verb stem. This ended up creating four fairly-regular categories: stems ending in -ā, with infinitives in -āre; stems ending in -ē, with infinitives in -ēre; stems ending in -ī, with infinitives in -īre; and stems ending in anything else, with infinitives in -ere. Eventually, in Vulgar Latin/Proto-Romance, this fourth "catch-all" group got split up and its verbs redistributed among the others, giving the familiar Spanish categories of -ar, -er, -ir.
So, the real question is, where did these four different "flavors" of stems come from?
Well, in Proto-Indo-European, there were quite a lot of different ways to turn a basic root into a verb stem. Most of these involved suffixes of some sort, so these suffixes determined the ending of the stem that was passed down to Latin:
- "Root verbs" (no suffix) generally ended in consonants, so they went into the -ere class
- Verbs suffixed with *-ye ended up in the -ere or -īre classes
- Verbs suffixed with *-eh₁ ended up in the -ēre class
- Verbs suffixed with *-eh₂ ended up in the -āre class
- Verbs suffixed with anything else went into the -ere class
So for most verbs, which class they ended up in, depended on which method was used to turn the root into a verb in Proto-Indo-European. Sometimes this affected the meaning, but sometimes it didn't, and it was just an accident of fate that the verb formed with *-ye caught on and the one formed with *-eh₁ didn't.