I'm drawing a lot on this summarized history of the Spanish verb.
Spanish first-conjugation verbs (-ar verbs) descend from Latin -āre. The phonetical evolution of these verbs from Latin to Romance was fairly straightforward. Most of the irregulars in this conjugation class belong to a well-known type of "regular irregulars", which alternate e with ie (like cerrar) and o with ue (like contar) following a simple rule. (This diphthongization has to do with the historical source of the vowel in Latin, and is not exclusive of the -ar class.)
Spanish second and third conjugations (-er and -ir verbs) descend from the Latin conjugations in -ĕre, -ēre, -īre. The first two were merged early in Ibero-Romance, and some verbs then shifted from -er to -ir (a few went the other way). These two conjugations share very similar inflections.
The -er/-ir verbs did not evolve phonetically as neatly as the others because in the original Latin form there was an unstressed front vowel (/e/ or /i/) between the verbal root and the ending in some forms. This vowel regularly turned into a yod or semivowel (/j/, like English y), this being part of a general tendency in Spanish to turn consecutive vowels in hiatus into diphthongs ([eo] > [jo], [ea] > [ja], etc.).
This yod had all sorts of effects on nearby sounds. In the case of the -ir verbs, if there was a tense mid vowel in the root (/e/ or /o/) the yod raised it, turning /e/ into /i/ and /o/ into /u/. In Romance linguistics this is customarily called metaphony; it's a form of anticipatory assimilation. Metaphony is common in Romance languages and elsewhere.
The yod dissapeared after this root-vowel raising. Thus we got patterns like those in medir ~ mido ~ midió.
If there was a lax mid vowel (/ɛ/ or /ɔ/, descended from Latin short vowels), the pattern described above combined with the diphthongization already mentioned, producing a three-way alternation: morir ~ muero ~ murió, sentir ~ siento ~ sintió.
Verbs where the root ended in a velar consonant (/k/ or /g/) palatalized this consonant before front vowels (/e/ or /i/), which added other irregularities. Thus for decir, the underlying stem dec- (with metaphony, dic-) gave both [diʦe] (modern dice) and [diko] (modern digo).
There's a whole other story with the verbs that insert -g- in the first person, like tener and valer, again because of the presence of a front vowel in the ending.
Add generous doses of analogy, regularizing verbs that had only a few irregular forms, or extending the irregular forms of some very common verbs to others of the same general phonetic shape.
These are some of the factors are what made the second and third conjugations so riddled with irregularities.