In short, no, subletting is not illegal. If you obtain the necessary permission from your landlord and abide by the subletting laws of your state and municipal government, it is legal to sublease an apartment, house, room, or any other property that you are currently renting.
Some state and local laws allow you to sublet a property you rent, even if your lease explicitly forbids it. However, landlords can evict or even sue sublessors and sub-tenants in other states if they don’t receive permission to sublet. Always check the laws in your area.