Mat 7:21-23 (ESVST) 21 " Not everyone who says to me, ' Lord, Lord, ' will enter the kingdom of heaven, but the one who does the will of my Father who is in heaven. 22 On that day many will say to me, ' Lord, Lord, did we not prophesy in your name, and cast out demons in your name, and do many mighty works in your name? ' 23 And then will I declare to them, 'I never knew you; depart from me, you workers of lawlessness. '
This is my understanding of Jesus' words,
These people know who Jesus is and they know the power in His name. In His name they cast out demons, they prophesied, they did many mighty works. "THEY" knew Jesus, but Jesus didn't know them. Why didn't Jesus know them if they were doing all these good works in His name? Because they didn't have a "personal" relationship with Him! It's not enough to just know Who He is or what He has done, we have to have an intimate, personal relationship with "HIM".
That is why Jesus said,
Mat 15:7-9 (ESVST) 7 You hypocrites! Well did Isaiah prophesy of you, when he said: 8 "'This people honors me with their lips, but their heart is far from me; 9 in vain do they worship me, teaching as doctrines the commandments of men.'"