These many people did not place their faith (belief, trust, reliance) in Jesus Christ alone for salvation, but instead were trusting in their works to save them. Jesus NEVER knew them which means they were NEVER saved. (Matthew 7:23)
In regards to Matthew 7:21-23, (I will never forget, prior to my conversion several years ago while still attending the Roman Catholic church) I read Matthew 7:22 and thought to myself, wow! These many people accomplished all of that, "prophesied in His name, cast out demons, and did many wonderful works" but that still was not "good enough?" Then I thought to myself at that time, how am I going to "top that" and be "good enough?" Such is the mindset of someone who believes that obtaining salvation is based on works.
Matthew 7:21 - Not everyone who says to Me, 'Lord, Lord,' will enter the kingdom of heaven, but he who does the will of My Father who is in heaven will enter.
*John 6:40 - For my Father’s will is that everyone who looks to the Son and believes in Him shall have eternal life, and I will raise them up at the last day.
If these many people had a lot of faith on Him, then they would not have been looking to their works for salvation and Jesus would not have said, "I NEVER knew you." John 3:18 - He who believes on Him is not condemned.. These many people will be condemned. What does that tell you about their so called faith? These many people (unbelievers) in Matthew 7:22 had the wrong foundation. They were trusting in their works to save them and NOT IN CHRIST ALONE. Jesus NEVER knew them which means they were NEVER saved. Their hearts were not right with God, so their "attempted external obedience" (apart from the righteousness of God which is by faith and the blood of Christ) was stained with sin, so their righteousness amounted to filthy rags. *Seeking salvation by works is not the will of the Father.
They lacked authentic faith because Jesus NEVER knew them. John 17:3 - And this is eternal life, that they may know You, the only true God, and Jesus Christ whom You have sent. The term "know" implies intimate, experiential knowledge, through a relationship with Him, not merely theoretical knowledge. These many people were not true converts.
*Without faith it's impossible to please God no matter how many "alleged" wonderful works that these many people set out to conjure up through the flesh in a vain effort to obtain salvation based on works. This is why Jesus referred to these many people as "workers of iniquity." God does not see the righteousness of God through faith in Jesus (Romans 4:5-6; Philippians 3:9) in unbelievers, but instead He see's all of their sins which remain and have not been washed away by the blood of Christ. Hence, "workers of iniquity."