A rent increase is illegal if your landlord tries to increase the rent before your lease is up for renewal without your consent, or if they're doing it as a form of discrimination or retaliation. For example, a landlord can never increase rent in an attempt to discriminate against anyone protected by the Fair Housing Act.
Most states have laws that specifically protect tenants from retaliatory rent increases as well. This means that if you made a valid complaint about your unit’s habitability or you call the police on your landlord for trying to illegally evict you, and then they spike up the rent - they're in the wrong and you could sue for damages.
If you live in a rent-stabilized or rent-controlled building, your landlord cannot increase your rent just because they want to, and if they do then it would also be an illegal rent increase.