This bit of scripture needs to be viewed not literally, but from the spiritual (specifically, the anagogical) sense.
Refer to Catechism of the Catholic Church (roughly from Paragraph 100 through 117).
If we don’t love God and our neighbor, and if we don’t follow Jesus’ commands, then we don’t really belong to His side, or even His Body. We don’t know Him as a friend or brother, or as God, or as Our Lord and Savior.
If we know and love Jesus, we see Him in our neighbors, and we help Him by serving their needs. If we don’t love them (as an act of will that leads us to help,although not always emotionally), we don’t love Him.
So then, even if we do all the churchy things and receive Sacraments during our lives, He won’t “know” us at the Last Judgment. We will have no claim on Him. We are His enemies, according to our own acts and choices.
OTOH, Jesus looks very kindly on even small acts of love to strangers, even if the person does them slowly and reluctantly. That is very hopeful.
To know God better and to follow Him, we have to remember to show love to our neighbors through our actions. That’s it. Pretty simple. Pretty sad to go to Hell for not doing simple things.
So help your neighbor, and don’t go to Hell.
Regard Judgement Day
Truly, truly, I say to you, he who believes has eternal life. I am the bread of life. Your fathers ate the manna in the wilderness, and they died. This is the bread which comes down from heaven, that a man may eat of it and not die. I am the living bread which came down from heaven; if any one eats of this bread, he will live for ever; and the bread which I shall give for the life of the world is my flesh.”
The Jews then disputed among themselves, saying, “How can this man give us his flesh to eat?” So Jesus said to them, “Truly, truly, I say to you, unless you eat the flesh of the Son of man and drink his blood, you have no life in you; he who eats my flesh and drinks my blood has eternal life, and I will raise him up at the last day. For my flesh is food indeed, and my blood is drink indeed. He who eats my flesh and drinks my blood abides in me, and I in him. As the living Father sent me, and I live because of the Father, so he who eats me will live because of me. This is the bread which came down from heaven, not such as the fathers ate and died; he who eats this bread will live for ever.” - John 6:53-59
‘I will be with you always’ (the Sacraments)…‘if you love me you will obey my commandments’ (obedience)
How does this… connect with the OP ?
OP — What Does Jesus Mean When He Says to Some, “I do not know you.”
*Many people will say to me on Judgement day, “Lord, Lord, did we not prophesy in your name and in your name drive out demons and in your name perform many miracles?”
Then I will tell them plainly, “I never knew you. Away from me, you evildoers!”* (Matt 7:22-23)
Jesus means… that there are those who - in spite of all their lip-service et cetera,
do/did not actually Love their Neighbor in Action…
The truest and fullest personal relationship we can have with Jesus is through the Catholic Church…the Sacraments, especially the Eucharist ( I will be with you always) and obedience to His teachings (Doctrine) (if you love me you will obey my commandments.)
However, the Eucharist is not magic, and cannot give grace to one who does not have the proper disposition. So Matthew 25 presents no contradiction, but rather a prerequisite. One must first be in such a state as to receive Jesus, which means we must also be in a state that receives Jesus in our neighbor in need, the stranger, the immigrant, the hungry, the poor, and the prisoner.
D-R Bible, Haydock Commentary:
Ver. 22. Have not we prophesied in thy name? The gift of prophecy, and of doing miracles, may sometimes be granted to bad men, as to Caiphas, and Balaam. (Witham) — Under the name of prophets, the Hebrews comprised not only such as predicted future events, but also in general all such as gave themselves out for inspired, or who undertook teaching and interpreting the holy Scriptures; and here by prophesying is understood, in a general acceptation, all public functions, predicting futurity, expounding Scripture, instructing the people, preaching, &c. (Bible de Vence)
Ver. 23. So as to approve and reward your works. Here he shews that even prophecy and miracles will not save us without good works. (Menochius) — How much less will faith, unassisted by good works, preserve us from condemnation. (Haydock) — The gift of miracles is bestowed on men not for their own good, but for the advantage of others. We must not then be surprised if men, who had indeed faith in Christ, but whose lives did not correspond with their faith, should be honoured with these extraordinary gifts, since the Almighty sometimes employs as his instruments in working similar wonders, men destitute both of faith and virtue. Balaam, void of faith and probity, still by the will of God, prophesied for the advantage of others. To Pharao and Nabuchodonosor were revealed future events of the greatest moment; and the wicked Judas himself cast out devils. Therefore St. Paul said, “if I had all faith so as to remove mountains, and if I knew all mysteries, and was possessed of all wisdom, but had not charity, I am nothing.” (St. Chrysostom, Hom. xv.)
Jesus teaches that when we help the least of people we help Him. Those on the right did that, the people on the left did not. So He never knew those on the left.
… via the FAITH which JESUS asks us to receive…
The passage also means Jesus knows you. He knows you when you know Him with true love or when you are just pretending.
Obey God’s Command _ To Love! In Action!
This way, Our Lord Jesus will say: I Know You’ve been Obedient! Welcome In!