Subletting someone's apartment is a relatively common practice in New York City. Maybe you need a place to stay for the summer before you start classes at NYU and you find a friend of a friend who's off shooting a movie in Europe for the summer. It's a win-win, right? You get an apartment close to your job and he gets reimbursed for his rent while he's away. Some tenants also sublet their New York apartments even though they're currently living upstate because they don't want to lose them.
However, what if you find out that while you've been paying rent to the primary tenant, that tenant isn't paying the landlord? Can you be required to leave the apartment even though you've kept up your end of the bargain? It depends.
If it's a "market rate" apartment -- meaning that there are no rent restrictions on it -- then you as the subletter could be subject to eviction along with the primary tenant who hasn't been paying their rent. However, even if you're paying the primary tenant market rate, it may be a rent-regulated apartment. Therefore, it's wise to find out by checking with the New York Division of Housing and Community Renewal.
If the apartment is rent-regulated, you may be able to claim that you're an "illusory tenant." That means you're essentially the prime tenant since the actual tenant is making a profit by subletting the apartment at a higher rate. If you are willing to go to court with that defense and you win, you could end up as the new tenant paying the regulated rate.
If the apartment isn't rent-regulated, or you don't want to go through a court battle because you have no interest in making the apartment your long-term home, you can talk to the landlord or managing agent. Explain that you've been paying rent to the primary tenant, and provide proof, such as canceled checks. The landlord may let you stay on and pay them the rent directly. Of course, they may decide to evict both you and the primary tenant.
If you're contemplating going to go to court -- either to claim illusory tenancy or to get your money back from the primary tenant -- it's wise to seek experienced legal guidance.