Indeed, not only have lay men (e.g. John XIX 1024-1032) been elected pope, but deacons (e.g. Leo X 1513-1521), and in the early centuries, many priests of the diocese of Rome. Of course, many bishops have been elected, as well, especially since the appointment of the bishops of dioceses surrounding Rome, and from elsewhere, as Cardinals (advisors and electors of the popes).
So, who can be elected Pope?
In fact, any baptized Catholic male is canonically eligible to be elected. Dr. Edward Peters JCD, has an excellent commentary on this point on his canon law blog, canonlawblog.wordpress.com, which is reprinted below with his permission.
It's been more than 500 years since a non-cardinal was elected pope, but then, it's been more than 500 years since a pope resigned, so, one moves cautiously to our question, who is eligible to be elected pope?
Turns out, lots of people.
Canon 332 § 1 of the 1983 Code simply states that one already a bishop (n.b.: not necessarily a cardinal) who accepts legitimate papal election becomes pope immediately. One who is not yet a bishop (and the Church has elected several non-bishops to the papacy) can accept election, but must be immediately consecrated bishop. By implication, that would seem to require that a papabile (a) be male, and be willing (b) to be baptized, (c) ordained deacon, priest, and bishop, and (d) have the use of reason in order to accept election and, if necessary, holy orders.
Or does it?
Skirting close to some important ecclesiological questions about ecclesiastical power and holy orders, the standard authors elaborate on the above-cited qualifications. In presenting them, allow me to underscore that canon law is an international legal system:
Capello, Summa (1951) I: 278: "Valide potest eligi quilibet vir, qui sit sui compos, capax acceptandi, membrum Ecclesiae, etiam laicus; licite qui omnibus qualitatibus praeditus sit, ita ut inter omnes dignior censeatur.
Sipos, Enchiridion (1954) 153: "Eligi potest quodlibet masculinum, usu rationis pollens, membrum Ecclesiae. Invalide ergo eligerentur foeminae, infantes, habituali amentia laborantes, non baptizati, haeretici, schismatici."
(Claeys-Boùùaert), Traité (1954) I: 375: "Sont éligibles tous ceux qui, de droit divin ou ecclésiastique, ne sont pas exclus. Sont exclus les femmes, les enfants, les déments, les non baptizés, les hérétiques et les schismatiques. Un laïque peut être élu validement. Il convient toutefois que l'élu soit pris parmi les cardinaux."
Eichmann-Mörsdorf, Kirchenrecht (1959) I: 356: "Über die Wählbarkeit fehlen nähere Bestimmungen. Grundsätzlich kann jedes männliche, vernunftbegabte Kirchenglied gewählt werden, also auch ein Laie."
Abbo-Hannan, Sacred Canons (1960) I: 284-285: "For the validity of the papal election it suffices that the candidate elected be of the male sex, a baptized Catholic, capable of accepting the election and of exercising the jurisdiction attached to the office. For its lawfulness, that candidate must be elected who is considered the best qualified."
(Alonso-Lobo), Comentarios (1963) I: 565: "Por derecho divino es elegible cualquier varón bautizado que tenga el uso de razón suficiente para aceptar la elección y ejercer la potestad de jurisdicción, aunque no sea todavía clérigo; de todas formas, los Cardenales no pueden elegir lícitamente a cualquiera, sino que deben fijarse en el que crean más digno."
Interesting. Most commentators consider being a baptized (indeed, baptized Catholic) male with the use of reason as necessary for the validity of the election itself. So, one's capacity and willingness to be ordained suffices for validity of election, but not one's willingness to be baptized and ordained. I wonder what it is about being a member of the Mystical Body of Christ at the time of election that has most commentators talking about it impacting the validity of election?
Oh well, it's going to be a cardinal, so the question's moot.
© 2013, Dr. Edward Peters JCD. Used with permission.
Note that Dr. Peters makes reference to canonists who commented on the 1917 Code of Canon Law, and thus to the long canonical tradition on the matter. The relevant canons didn't change much between 1917 and 1983, and there isn't yet much commentary on them, at least not any based on new historical experience.
In an addendum to the question, Dr. Peters also answers the question whether a married man could be elected. He writes that he could, noting that in ancient times married men were often elected to become bishops, but then "ceased living as married men."
Practically, however, it is unlikely that anyone but a bishop, and then one who is a member of the College of Cardinals, would be elected, given the far-ranging sacred and human experience needed, as well as being known to the members of the College for these qualities. So, outside of truly exceptional circumstances, and an extraordinary individual, it is extremely unlikely that a layman, deacon or even a priest, would be elected Pope.
If a non-bishop were elected, and he accepted election, in addition to the practicalities of locating him and getting his consent, he would have to be raised through ordination to the episcopal order (by way of diaconal and priestly ordination, if necessary), at which time he acquires full and supreme authority over the universal Church (canon 332, 1; cf Universi dominici gregis 88).