The Church militant will always exist until the end of time. Matthew 16:18:
And I say to thee: That thou art Peter; and upon this rock I will build my church, and the gates of hell shall not prevail against it.
The papacy, and thus also the Church, will last until the end of the world, as the First Vatican Council said in Pastor Æternus (July 18, 1870), ch. II, "On the Perpetuity of the Primacy of blessed Peter in the Roman Pontiffs":
That which the Prince of Shepherds and great Shepherd of the sheep, Jesus Christ our Lord, established in the person of the blessed Apostle Peter to secure the perpetual welfare and lasting good of the Church, must, by the same institution, necessarily remain unceasingly in the Church; which, being founded upon the Rock, will stand firm to the end of the world.
Even if the vast majority of the hierarchy apostatizes, which happened during the Arian crisis,* the Church will still exist.
*(St. Athanasius remaining one of the few Catholics)
St. Vincent of Lerins, in his Commonitorium book 1, chapter 2 (English translation of excerpt), foresaw the possibility of heresy taking over almost the entire Church, in which case there would remain few non-apostate, true members of the Church:
Also in the Catholic Church itself we take great care that we hold that which has been believed everywhere, always, by all. For that is truly and properly Catholic, as the very force and meaning of the word shows, which comprehends everything almost universally. And we shall observe this rule if we follow universality, antiquity, consent. We shall follow universality if we confess that one Faith to be true which the whole Church throughout the world confesses; antiquity if we in no wise depart from those interpretations which it is plain that our ancestors and fathers proclaimed; consent if in antiquity itself we eagerly follow the definitions and beliefs of all, or certainly nearly all, priests and doctors alike.
What, then, will the Catholic Christian do if any part of the Church has cut itself off from the communion of the Universal Faith? What surely but prefer the soundness of the whole body to a pestilent and corrupt member?
What if some novel contagion seek to infect the whole Church, and not merely a small portion of it? Then he will take care to cling to antiquity, which cannot now be led astray by any novel deceit.
What if in antiquity itself error be detected on the part of two or three men, or perhaps of a city, or even of a province? Then he will look to it that he prefer the decrees of an ancient General Council, if such there be, to the rashness and ignorance of a few.
But what if some error spring up concerning which nothing of this kind is to be found? Then he must take pains to find out and compare the opinions of the ancients, provided, of course, that such remained in the communion and faith of the One Catholic Church, although they lived in different times and places, conspicuous and approved teachers; and whatever he shall find to have been held, written and taught, not by one or two only, but by all equally and with one consent, openly, frequently and persistently, that he must understand is to be believed by himself also without the slightest hesitation.