These people in Matt. 7:21-23 did NOT cast out demons. How do I know that? Jesus said to them "depart from me, I never knew you." These people were not saved, therefore, the Holy Spirit could not work through them. They thought they had cast out demons. But Jesus tells them, not only did you do nothing in my name, you have also lost your souls.
Their faith was not properly placed in Jesus Christ. All Jesus asks of us is to believe in His Son, then the works will follow. These people put the cart before the mule, so to speak. They never knew Jesus and paid the ultimate price for it.