And I tell you, you are Peter, and on this rock I will build my church, and the gates of hell shall not prevail against it. - Matthew 16:18
Is it "this rock" or "my church" that the gates of hell shall not prevail against?
και ἐπι ταύτῃ τῇ πέτρᾳ οἰκοδομήσω μου την ἐκκλησίαν, και πύλαι Ἅιδου οὐ κατισχύσουσιν αὐτῆς
On this basis alone, the antecedent of "it" can be the rock or the church. The sage Origen of Alexandria himself acknowledged the grammatical ambiguity:
But what is the ‘it’? Is it the rock upon which Christ builds the church, or is it the church? For the phrase is ambiguous. (Commentary on Matthew 12.11)
This is (probably) the most contested passage in the New Testament. It is a bedrock (no pun intended) claim to apostolic succession for the Roman Catholic Church. It has prompted the politically devious (I'm looking at you, Bismarck) to try to discredit the whole Gospel of Matthew to get rid of this passage and undermine the Roman Catholic Church (see scholarship by Farmer here). The Reformers, and many who followed them, have rejected the apostolic succession interpretation of the passage.
If one is already committed to a particular view for or against apostolic succession, nothing I say here is going to move the needle on this matter.
The Gates of Hades
Although many English translations render the phrase "the gates of hell", the Greek word used is not "hell", but "Hades" (not Tartarus, not Gehenna, not the lake of fire). Hades is the place of the dead.
As Michael16 summarized in this post:
Entering the gates of Hades as a metaphor for the experience of death begins in classical writers with Homer who describes dying as passing the gates of Hades. (Iliad 5.646) The gates of Hades as the experience of death are also used by Aeschylus, while Euripides describes a phantom from the gates of darkness (skotou pylas) where Hades dwells. He also has the dying person beholding the gates of death. Theocritus addressed Artemas as one who moves the adamant at the door of Hades. Jack P. Lewis, Gates of Hell, JETS 38/3 (September 1995)
Dying experience as passing [sic] the gates of hades or a dying man seeing the gates of hades shows that the gates are progressive degrees of stages towards the deepest end.
On a practical or biblical basis for the meaning of the plural gates of hell or hades, I gather can be understood as the various pangs and powers of death. As the RSV, NEB, REB uses powers of death.
Victory Over Death
Jesus tells the disciples that the power of death will not overcome His work. He does not indicate that death will not happen; rather, as later taught by Paul, death's victory will not be permanent. People will enter the gates of death, but they will not stay there forever:
55 O death, where is thy sting? O grave, where is thy victory?
56 The sting of death is sin; and the strength of sin is the law.
57 But thanks be to God, which giveth us the victory through our Lord Jesus Christ. (1 Cor 15:55-57, see also v22)
It is Jesus who grants victory over death; it is the revelatory testimony of Jesus as the Messiah & Son of God (borne by Peter in v16) that is being praised in Matthew 16:17. The church helps people access the power upon which it is built, church leaders can be delegated keys of authority to carry out God's work on the earth, but it is neither the church nor Peter that overcomes death: it is Jesus the Messiah the Son of God, whose identity and mission was made known to Peter by revelation from God.
The power of death will not overcome the rock.
Post-script--a more expansive view
One could conceivably argue that the power of death will not overcome that which is built upon the rock as well. That's fair. But if X is built upon the rock, the power of death doesn't overcome X not on X's own merit, but specifically because X is built upon the rock. It is the rock that gives stability.
The Gates of Hades is an actual temple grotto at Caesarea Philippi, which would have been seen as a portal to the underworld, much like the well at En-Dor in the northern kingdom, and Kutha in Babylonia.
Hades was believed to be an underground fortress of the dead, similar to Sheol, which had bars and gates on it. Those who entered would not normally return. Various spiritual deities would guard the dead and some, like the Rephaim, would welcome the dead royalty (e.g. Isaiah 14 and the king of Babylon).
The point of Jesus saying he would build his church and the gates of Hades wouldn't withstand it is his assault on the forces keeping humanity captive to Death. Gates are a defensive mechanism, not an offensive one - no one brings the city gates to a battlefield.
As such, he would build his church upon the victory won in the underworld over death - death has lost its sting. Whilst we all die, the resurrection means we can have the final victory over death in him. Symbolically it's also the very rock upon which they stood at the time he proclaimed it. And as the rock is also symbolic for himself, and no other foundation can be built upon, it's also referring to him.