Yes. The legality of an apartment has nothing whatsoever to do with the eviction process. Illegal apartments are an issue between the town and the landlord. If a landlord is caught by the town with an illegal apartment, a landlord may pay fines, but that does not affect the landlord-tenant relationship. A tenant is still obligated to pay rent, and a landlord of an illegal apartment can still evict them.
You may enjoy this article on the penalties for renting an illegal apartment on Long Island.