Mat 7:22 Many will say to me in that day, Lord, Lord, did we not prophesy by thy name, and by thy name cast out demons, and by thy name do many mighty works?
Mat 7:23 And then will I profess unto them, I never knew you: depart from me, ye that work iniquity. (ASV)
The emphasis here is on doing the will of Father God and truly knowing Jesus. The word for know here is ginosko which is involves personal knowing not oida which is just to see or perceive. Many that go to churches see and perceive and practice a form of godliness, but it's power does not impact them because they really have no personal relationship with Jesus.
Jesus was saying here that that will not fly. It will not work. Even if you're doing so called 'good works' or perceived 'good works' it doesn't matter. Motive and obedience to what Jesus's Spirit says is key to any works being good. God is good alone. If a work was not authored by Him, then it isn't good anyway.
Some people confuse the institutional churches that men have created for vehicles and purposes (in the American tradition) for the actual Church that Jesus said, "I will build my church(ekklessia-called out ones or gathering) and the gates of hell will not prevail against it." This church or ekklessia consists of every truly born from above believer who experientially knows Jesus. If we are trying to defend institutions that men have built, then we will ultimately fall, because they will ultimately fall in some way.
There will be no Southern Baptists, Church of Christ-ians, Methodists, etc. in heaven but only followers of Christ period. They will seize to be partisan in heaven and named after man's vehicle. If we happen to attend and be a part of man's vehicle we need to be aware that that is what it is. I'm not saying that some of Christ's sheep don't meet in that building, but I'm saying that that building in NO way is the Ekklessia that Jesus talked about.