The occultation of the Imam left a question Sunni Islam never had to answer: who exercises his authority in his absence? The Twelver reply, worked out over centuries, produced the nearest thing in Islam to a clerical hierarchy.
The mujtahid is a scholar qualified to derive rulings independently, and every ordinary Twelver is expected to follow a living one, to practise taqlid, emulation, of a marja al-taqlid, a source of emulation. Those sources are not appointed by anyone; standing is acquired through scholarship, students, published legal manuals and the confidence of the faithful, and it is confirmed in practice by the willingness of believers to send khums, a religious tax of a fifth, directly to a marja rather than to any state. That independent revenue is why the senior clergy of Najaf and Qom have been able to act as a power centre independent of governments, and it has no Sunni equivalent.
The senior ranks, ayatollah, grand ayatollah, are honorifics conferred by reputation rather than by any office, which means the structure is a hierarchy of recognition rather than of appointment.
The doctrine that transformed all this politically is wilayat al-faqih, guardianship of the jurist, in the expansive form given it by Ruhollah Khomeini and made the constitutional basis of the Islamic Republic of Iran after 1979. It holds that in the Imam's absence a qualified jurist should exercise political rule. It is a minority position among Twelver scholars, not a settled Shia doctrine, and it is rejected in its political form by the quietist school associated with Najaf and with Ali al-Sistani, which holds that clerics should guide rather than govern. That disagreement between Qom and Najaf is the most consequential unresolved argument inside Twelver Shiism.