The OP is right - it appears Matthew 7:22 implies:
Those who have invoked the power of God for prophecy, exorcism and
miracles may still be condemned on the day of Judgement.
And from that one can conclude...
Therefore, NO assurance of salvation can be given, even when invoking
the powerful name of Jesus Christ.
And even more scandalous in this verse is the use of the Greek word...
“polloi” (“multitude”, “great in number”, “numerous”, “many”).
If Jesus is speaking of false-Christians or pretend-Christians, the use of “polloi” insinuates that the church contains a “multitude”, “great in number”, “numerous” amount of imposters.
However, drawing such implications puts Matthew 7:22 at odds with Joel 2:32 and contradicts most of the Gospel narrative. Therefore there must be more depth in the meaning of this verse.
Seeing as how the Gospel of Matthew is primarily a message to the Jews. And the sermon on the mount is during the start of Jesus’s ministry, a time that was exclusively focused on ministry to the Jews. And since Jesus’s sermon on the mount is delivered to a Jewish audience and primarily deals with refuting and correcting the false teachings of the Jewish religious leadership.
It is reasonable to contextually assert that this verse, with other verses in this discourse, are addressing directly the “Old Covenant People.”
Interpreting the “polloi” multitude to be a “multitude of those who rely on diligent law-keeping for salvation (ie Pharisees)” the next verse makes this declaration:
And then will I declare to them, ‘I never knew you; depart from me,
you workers of lawlessness.’ - Matthew 7:23 (ESV)
In the Greek, the word translated “lawlessness” is the cognate “anomia” made up of...
- 1 “a” which means "no" and
- 3551 “nómos” which means "law"
Literally it means "no-law"
So in this context (Jesus addressing the Jewish establishment) this passage declares:
...depart from me, you workers of
[imperfect law-keeping, that is in effect]
no-law
A teaching similar to Mt 7:22 is given by Jesus in Luke 13...
“Once the head of the house gets up and shuts the door, then you will
stand outside and start to knock on the door and beg him, ‘Lord, let
us in!’ But he will answer you, ‘I don’t know where you come from.’
Then you will begin to say, ‘We ate and drank in your presence, and
you taught in our streets.’ But he will reply, ‘I don’t know where you
come from! Go away from me, all you evildoers!’ There will be weeping
and gnashing of teeth when you see Abraham, Isaac, Jacob, and all the
prophets in the kingdom of God but you yourselves thrown out. - Luke
13:24-28 (NET)
If this is the same teaching, we know for certain it is specifically directed at the “Old Covenant People” because in the very next verse Jesus shifts the subject to speak of the “New Covenant People”...
Then people will come from east and west, and from north and south,
and take their places at the banquet table in the kingdom of God. -
Luke 13:29 (NET)
Paul re-affirms what Jesus says about the failure of those seeking justification through the law…
For there is no partiality with God. ...and all who have sinned under
the law will be judged by the law. ...on the day when God will judge
the secrets of human hearts, according to my gospel through Christ
Jesus. - Romans 2:11 - 16 (selected)
And Paul further says this...
...You who preach against stealing, do you steal? You who tell others
not to commit adultery, do you commit adultery? You who abhor idols,
do you rob temples? You who boast in the law dishonor God by
transgressing the law! For just as it is written, “the name of God is
being blasphemed among the Gentiles because of you.” Romans 2:21-23
(NET)
Clearly Jesus is directing His admonition squarely at the religious who try to earn salvation through law-keeping, as the Mt 7:22-23 warning would have no application to a people who live by faith under a Covenant of grace and mercy.
Joel too prophesies of this future Covenant of grace when he speaks of a “future time,” saying...
“And it shall come to pass....” Joel 2:32
Before his prophecy continues with...
“...everyone who calls on the name of the Lord shall be saved” Joel
2:32