They indeed had a long pre-history since Late Antiquity until the final establishment of the 'College of Cardinals' in late 11th and 12th century with their most popular function now as the electorate body of the next pope.
I suppose the three ranks of the cardinals, namely bishop, priest, and deacon, is relatively well-known, and now titular, but they had originally been the the high rank clergy in the church in and nearby Rome in Late Antiquity and Early Middle Ages. They occasionally assisted the Pope (or, the bishop of Rome might have been more accurate word then) during the liturgy in major churches in City Rome, and also involved the papal (or episcopal) election of Rome, together with other clergy. As a neighboring (sub-urban=Roman) diocese of bishop cardinals, Ostia, Tusculum, Albano, Sabina and so one were titular bishop seats reserved for these high-ranking cardinals.
In the 8th century, Pope Stephen II (III) issued a decree that restrict the right to vote for the next pope to a group of the clergy against the lay Roman nobility, and it is also said that this decree was the origin of cardinals. In theory and word (cardinales<Latin cardo, the hinge of the door [of the papal residence/ church]), this statement (also in Wikipedia) is correct, and the relationship between the pope and these 'cardinales' got close in course of time after that: 7 bishop cardinals were now expected to attend the service conducted in St. Peter basilica.
It was not until the middle of the 11th century that the membership of the cardinal became really important in the papal election as well as the papal representative (as papal legate a latere, dispatched across Western Europe to promote the local church reform), though. Needless to say that the reformed Papacy now had a renewed interest in guaranteeing as well as extending their authority both in and out or Rome, and the cardinals also became the assistant/ representative of the papal assertion of the church governance in whole Latin Christendom. This historical trend must also have consolidated the social status of cardinals.
After the beginning of the 12th century, the conflict of opinion on who should be the next pope officially occurred within the college of cardinals, even though the rivalry between opposing Roman aristocrat families was often the real problem, like that between the Frangipanis and the Pierleonis in the Schism of Antipope Anacletus. In such a case, Roman aristocrats primarily had a say on the papal election by way of the cardinal from their family, usually not in form of more direct intervention. At that phase, we'd say the college of cardinals had been firmly established as an institution by then.
A series of papal decrees issued in the 11th and 12th centuries' popes, such as that of 1059 (linked to the classical English translation), also contributed to the establishment of the papal election system as well as the cardinals. Based on this institutional development during the High Middle Ages, the practice of 'conclave' during the papal election firstly appeared in the 13th century.
References: