Question: "Does Matthew 7:21-23 mean that believers can lose salvation?"
Answer: The two questions most frequently asked about Matthew 7:21-23 come about because the verses seem to contradict two strongly held beliefs—one cannot lose his salvation and anyone who performs miracles must be from God. As we will see, one of these beliefs is based on scriptural truth and the other is not. While the true believer cannot lose his salvation, not all miracles are performed by true believers.
Jesus is speaking here near the end of His Sermon on the Mount (chapters 5—7). Beginning in verse 13, Jesus discourses at length on the subject of true faith vs. false professions, using the technique of contrast and comparison. Verses 13-14 describe two paths on which people walk through life, the broad road that leads to eternal destruction and the narrow path that leads to eternal life. He introduces here the concept of the "many" and the "few" that He will return to in verses 21-23.
In Matthew 7:15–18, He again contrasts the two types of people by using imagery well known to those in an agrarian culture—sheep/wolves, grapes/figs, thorn bushes/thistles, good trees/bad trees, and good fruit/bad fruit. Having established the idea of dichotomy in the minds of His hearers, He goes on to apply these truths to the spiritual state of all within His hearing. Jesus presents the two types of people who will come to Him on “that day,” meaning the last day, the day of judgment, the great day fixed by God and unknown to angels and men and which will be terrible to some and joyful to others. All will be seeking to enter the kingdom of heaven, but some will be turned away and will react in utter confusion and disappointment as what they thought was their “ticket” to heaven turns out to be worthless. These are those who prophesied in the name of Jesus, meaning either foretelling things to come or preaching the Word in His name. They have even performed miraculous acts such as driving out demons and perhaps healings and other miracles, but all to no avail. Their works were done for their own glory, not His, and were nothing more than “filthy rags” (Isaiah 64:6). In contrast, those who will enter heaven will not do so based on their miraculous achievements and accomplishments or works of any sort. They will have eternal life solely on the basis of obedience to the will of God (Matthew 7:21)—which is to believe in God’s Son.
But who are these unfortunate people and how can they do miracles unless they are doing them by God's power? We know several things about them from the text. First, we know there are many of them, because it is many who are on the broad road to eternal death, as compared to the relative few who have found the narrow path to eternal life. Second, they claim the name of Christ. These are not Muslims, Buddhists, or atheists. These are those who would gladly say, "I am a Christian." They work their works in Jesus' name. They pray and heal in Jesus' name. They preach and teach in Jesus' name. They build huge churches and ministries in Jesus' name. They claim a relationship with Him. But they are none of His. In fact, He sends them away, not with commendation for their good works in His name (and many good works are done in Jesus' name by false professors), but by disowning their every deed and word. He "never" knew them, nor did they ever truly know Him. They weren't Christians who lost their salvation. They were never part of the elect of God, chosen before the foundation of the world (Ephesians 1:4), set apart and sanctified by the Spirit (1 Corinthians 6:11; 2 Timothy 2:21), and justified by faith (Ephesians 2:8-9). The ones Christ puts away from Himself with harsh words of condemnation, calling them doers of evil, never came to Him in faith, despite their outward good works and miracles.
Then if these do not belong to God and never did, how do we explain their ability to perform miracles? Can those who do not have the Spirit actually cast out demons and perform miraculous healings and spectacular signs and wonders? There are two possible explanations for the ability of the ungodly to perform such acts. One is that some miracles are done by the power of Satan and his demonic host. We know that Satan is very crafty. Could there be times when Satan “stages” an exorcism in which an unbeliever commands a demon to leave, and the demon pretends to comply, for the purpose of creating fear in onlookers and causing them to trust in herbs, incantations, talismans, holy water, and relics, rather than in God? Could it be that the doomed men in Matthew 7 had been convinced, in their own minds, that they were serving Christ based on the (satanically orchestrated) “successes”? that the demon they thought they had authority over were only playing a game with them? that they considered themselves demon slayers, when all the while they were nothing more than demons’ dupes? Not all miracles come from God, and not all who perform miracles in the name of Jesus are truly His. The Lord calls the men in Matthew 7 “evildoers” because the miracles they performed had evil as their source, and they had been too blind to see it. Jesus warns us to be on guard against deception as the end times draw near (Matthew 24:24).
Second, it’s possible that God, in His sovereign will and for His purposes, will empower unbelievers to perform miraculous deeds. An example is Judas Iscariot who, along with the other disciples, preached the gospel and we assume healed the sick and performed other miracles. There is nothing to indicate that Judas didn’t have the same power as the other eleven, although he was never a true disciple of Christ. He was a deceiver and the “son of perdition” (John 17:12). If Judas did perform miracles, it was only because God saw fit to use him for His own glory, despite the condition of Judas’s heart.
Jesus goes on to describe those who will be able to call upon His name on the day of judgment. It will be those who hear His words and put them into practice, the same ones referred to in verse 21 as those who obediently do the will of the Father in heaven. True believers are the good trees that produce good fruit (Galatians 5:22-23), the true sheep who look to Christ, depend on Him, commit themselves to Him, trust in Him, and believe on Him for righteousness, salvation, and eternal life. These are the ones who will enter into the kingdom of heaven.