These many people in Matthew 7:22 will find out the hard way that everything they DID amounted to nothing because their hearts were not right with God. Jesus never knew these many people (verse 23) which means they were never saved.
The Pharisees thought they were keeping the commandments of God, but their hearts were not right with God.
Mark 7:6 - And He said to them, “Rightly did Isaiah prophesy of you hypocrites, as it is written:
‘This people honor Me with their lips,
But their heart is far away from Me.
If our heart is not right with God, then we are just going through the motions of a "nominal" Christian.
Luke 6:45 - A good man out of the good treasure of his heart brings forth good; and an evil man out of the evil treasure of his heart brings forth evil. For out of the abundance of the heart his mouth speaks.
Acts 8:21 - You have no part or portion in this matter, for your heart is not right before God.
Acts 15:8 - So God, who knows the heart, acknowledged them by giving them the Holy Spirit, just as He did to us, 9 and made no distinction between us and them, purifying their hearts by faith.
1 Corinthians 4:5 - Therefore do not go on passing judgment before the time but wait until the Lord comes who will both bring to light the things hidden in the darkness and disclose the motives of men’s hearts; and then each man’s praise will come to him from God.
Ephesians 3:17 - so that Christ may dwell in your hearts through faith; and that you, being rooted and grounded in love,
1 Thessalonians 2:4 - but just as we have been approved by God to be entrusted with the gospel, so we speak, not as pleasing men, but God who examines our hearts.
2 Thessalonians 3:5 - May the Lord direct your hearts into the love of God and into the steadfastness of Christ.