I'm going to supply a deeper insight into the workings of the highest ranks in Shia clerical establishment as well as some information about the scholarly credentials of Ayatollah Khomeini and Ayatollah Khamenei since there are some interesting and controversial aspects with regards to their religious career that are necessary to highlight for more skeptical or interested viewers.
Ijtihad and marja'iyat in Shia clerical system
Ayatollah Ruhollah Khomeini and Ayatollah Ali Khamenei are both widely recognized religious (and for many, political) authorities in the Shia world especially in Iran. This is implied by their honorific title, "Ayatollah ul-Udhma" (or "Grand Ayatollah" as in prevalent English usage) which indicates their position as both an accomplished scholar in fiqh (a quality that alone can earn the person the honorific title "Ayatollah") as well as their status as "marja' taqlid", that is, a source of emulation in religious practice for laymen. (But note that the modifier "Udhma" is often omitted for convenience where it has little contextual significance).
The first position is realized when a clerical student finishes all levels of religious studies to become a mujtahid or a sovereign scholar who no longer needs to follow the decrees of other scholars. This accomplishment is usually officially established by a certification granted by an established marja' after checking and endorsing notes taken by the student during the marja's lectures on advanced Shia fiqh books. However there are many other ways for recognition of ijtihad such as by testimonies of other accomplished scholars or marja's to the fiqh qualification of a high-level cleric, or more recently, as for Iranian clerics, by tests held by the Assembly of Religious Experts of Islamic Republic of Iran, a legal body that appoints and monitors the Iran Supreme Leader.
The latter higher position, i.e. marja'iyat refers to position of a scholar who in addition to his advanced scholarly qualifications has also developed some following among laymen who follow his fatwas as usually published in a treatise on juridical issues concerning laymen called risalat al-amaliyya. A scholar with this accomplishment is often honored by the title of "Ayatollah ul-Udhma" although whether this title is recognized beyond his circle of followers depends on the opinion of other scholars. This is also true about the previous title because other than scholarly qualifications (or "ijtihad"), the spiritual stature of a scholar is also influential in wider recognition of these titles due to their spiritual denotations (Ayatollah and Ayatollah Udhma, respectively mean "sign of Allah" and "grand sign of Allah").
The ijtihad and marija'yat of Khomeini and Khamenei
As I said there are some fascinating facts regarding the scholarly career of these two famous Shia religious leaders.
Ayatollah Khomeini's scholarly career
The marja'iyat (the status of being a marja') of the late Ayatollah Khomeini was acknowledged and publicized interestingly after his arrest in 1963 following his audacious protests against the secular policies of the Shah and his alliance with Israeli and US governments. It is said that during the years leading up to his political uprising, Khomeini had persistently refused to assert himself as a marja' by refusing to publish his juridical treatises and assuming any functions of marja'iyat after the death of the Grand Ayatollah Borojourdi in 1961 (who until then had been the exclusive marja of the entire Shia world). All this was despite the fact that he had been a close disciple of the passed Ayatollah, had held years of lectures on juridical, ethical, and even philosophical and mystical themes (disciplines reserved for the most intelligent and pious scholars) and had written several treatises in all these fields.
Imam's disciples have said that the reason for his refusal to assume marja'iat was that the Imam under the influence of His profound spiritual sincerity and strong Sufi/Mystic inclinations disliked the worldly aspects associated with marja'iya such as personal fame and gain, and particularly abhorred rivalries aimed at religious supremacy seen in the office of some contemporary scholars.
Interestingly, even later when his marja'iyat was ultimately acknowledged and publicized by the religious authorities in Qom it was not in response to his own request or initiative. Rather it was some of his close disciples who demanded the other religious authorities to acknowledge and publicize his marja'iya in order to save him from execution in the hands of Pahlavi regime. In the next years, during his exile in Turkey and then in Najaf, Iraq, Khomeini finished and integrated several of his fiqh treatises into a major juridical work called Tahrir al-Wasilah which was soon published. After the victory of 1979 Islamic Revolution in Iran, the treatise formed the basis of the Constitution of the newly-established Islamic Republic and its law.
Ayatollah Khamenei's scholarly career
As for Ayatollah Khamenei (who succeeded the late Imam as the Guardian Jurist and Supreme Leader of Iran), there have been some protests and doubts by a handful of scholars with respect to his qualifications in fiqh during his early years in office as the Supreme Leader, despite the fact that his official position already indicated that his advanced scholarly competence along with his piety and leadership capabilities had been acknowledged by a few dozen mujtahids and maraji' of the Assembly of Religious Experts who elected him as successor to the late Imam in 1989.
These doubts stemmed from the fact Ayatollah Khamenei had not authored any fiqh treatise and was not known to these scholars for having held lectures on fiqh in the past, two primary indicators of ijtihad. The main reason for this apparent shortcoming was Khamenei's long-time dedication to political revolutionary efforts before and after the establishment of the Islamic Republic in 1979 which for these skeptical scholars raised the question of when he could get the time to complete his religious studies. But to those scholars who knew him personally since his much younger years before the culmination of his political activities (when he used to hold fiqh lectures for advanced clerical students), his scholarly competence despite his lack of official certification was evident especially considering that he was one of the trusted disciples of Imam Khomeini who had also recommended him to his other disciples as the best candidate for his succession.
Notable among the scholars that publicly endorsed the ijtihad of Khamenei at the time were Ayatollah Muhammad Yazdi, Ayatollah Muhammad Fazel Lankarani, Ayatollah Ali Meshkini and Ayatollah Abdollah Javadi Amoli. Finally, 5 years after taking office, the Society of Lecturers of Qom Seminary consisting of a few dozen mujtahids and maraja's, published a statement announcing the names of scholars whom they could verify as worthy of marja'iat. The list (archived here on the Society's website) included the name of Ayatollah Ali Khamenei.
Furthermore, a series of discussion sessions on advanced fiqh issues held by the initiative of Khamenei himself shortly after assumption of office, provided a chance for directly testing his skills in treatment of juridical problems by the participating scholars. Today, the ijtihad and marja'iyat of Ayatollah Khamenei is recognized by virtually all prominent Shia clerical scholars.
I think this much is sufficient to establish the authority of these two influential Shia religious figures for any fairly skeptical researcher. Though, I could still provide many more interesting details with respects to their career that show elements of wisdom, piety and genius in this very rare class of religious scholars whose today's prominence is due to their success in uniting advanced scholarly and spiritual accomplishments with brilliant political leadership.
I used primarily Iranian official sources (in Farsi) to compose this. Some of them include:
English sources for more study: