The reference to the "keys of the kingdom of heaven" in Matthew 16:17-19 anticipates the polemic against the Pharisees in 23:13, which declares that "you shut (kleiete) the kingdom of heaven against men, for you neither enter (ouk eiserkhesthe) yourselves, nor allow those who would enter to go in (eiselthein)". There is a parallel logion in Thomas 39:1: "The Pharisees and the scribes have taken the keys of knowledge (tas kleidas tés gnoseós) and hidden (ekrupsan) them; they themselves have not entered (oute eisélthon), nor have they allowed to enter (oute eiserkhomenous) those who wish to". This version is a bit gnosticizing but the idea is still much the same. The idea is that the Pharisees, despite their valid halakhic authority (Matthew 23:2), do not lead by example (v. 3), and therefore stumble those who wish to enter the kingdom of heaven. The author views Jesus as finally fulfilling the Law in deed as it was intended (5:17), such that "unless your righteousness exceeds that of the scribes and Pharisees, you will never enter (ou mé eiselthéte) the kingdom of heaven" (5:20). These polemical texts portray Jesus, and in turn the apostles, as supplanting the Pharisees in halakhic authority — as the references to "binding and loosing" (a rabbinical concept) in 16:19 with reference to Peter and in 18:18 with reference to all the apostles show. Just as the post-Pharisee rabbis received their authority from Moses, so do the apostles receive their authority from the "new" Moses who fulfills the Torah.
That Peter is preeminent in ch. 16 reveals the prominence of Petrine authority in the Eastern community (imo Antioch in Syria) where Matthew originated. Thomas represents a separate Eastern tradition (imo originating in the Thomasine community in Edessa, Syria) that rejected Peter in favor of Thomas, cf. Thomas 13:4-6 where Thomas takes the place of Peter in Matthew 16. The Johannine community in Asia Minor similarly esteemed the Beloved Disciple above Peter, although here the "Peter" may not be the Law-observant Peter of the Eastern tradition but the more Pauline antinomian Peter of the Western Roman tradition. In the second century AD, as Rome and other Christian centres vied for dominance (such as Antioch, Ephesus, Alexandria, etc.), apostolic figures were pitted against each other as vouchsaving each respective community's tradition. Although Matthew originated in a Law-observant Eastern Petrine community, it later was used by Western Christians as validating their own authority over against that of "Judaizing" Eastern Christianity (as attested in the prolific Pseudo-Clementine literature which, in turn, co-opted one of the founders of the Roman church for validating their own Eastern traditions about Peter). And in Egypt, Peter's authority was attacked by gnostic Christians via such texts as the Gospel of Mary which criticized Peter's understanding of the Savior. In short, an inner-Jewish polemic made against the Pharisees at a time when Jewish Christians were beginning to break from Judaism later contributed to efforts to deligitimize factions within Christianity.