It is illegal when related to the discrimination laws. It is also illegal when action is taken against you because you have exercised certain other rights. Laws against discrimination also protect employees against retaliation. For example, if you make a complaint about sexual harassment and your employer fires you for making the complaint, you may ask for your job back and for back pay. Certain other laws protect you from retaliation as well.
Workers have the right to join a union.
They also have the right to come together to ask the employer to improve conditions without being fired. Six women work together sewing sleeves on jackets. The room in which they work is always dark, and they have trouble seeing their work. They ask the manager to make the lighting system brighter, and they are fired. It is rare in a discrimination or retaliation case for an employer to come out and say that he or she is firing a worker because of her race or sex, for example.