If you are sharing an apartment with others and your name isn't on the lease, your legal rights to stay are very limited. Once the leaseholder moves out, or the lease terminates for any reason, the landlord need not accept you as a tenant. You can be evicted when the named tenant's occupancy agreement ends. That's because you are not a tenant, only a "licensee," or someone who is there by permission on a temporary basis.
If you believe you have more rights than this alone, perhaps because the landlord accepted rent from you that you paid in your own name, or orally told you that you could stay, this might give you a defense against eviction in Housing Court. However, for you to assert any rights you may have, you must sue in New York State Supreme Court, because the only suits tenants can bring against landlords in Housing Court are about repairs or illegal eviction.
Generally, unless you would have a right to stay as a long-term tenant, preferably in a rent-regulated unit, it is too expensive and complicated to be worth suing in Supreme Court over a right to stay in a rented apartment.
If you want maximal protection of your rights, have your name on the lease and get a Tenants' lawyer to review it before you sign it.
Attention New York City Tenants: Get more information on Landlord Tenant matters at the McAdams Law main site here. If you believe you may have succession rights to the apartment you're living in, you can learn more in the Succession Rights section.