Is it ever legal for an employer to hire based on gender, religion, age, etc.?
There are cases where being male or female, of a certain religion, or within a particular age range, may be what is known as a bona fide occupational qualification for a job.
In these situations, an employer may distinguish between employees based on otherwise protected status and hire accordingly. For example, only men may qualify for male roles in a movie, and only men or boys of a certain age for the role of a teenager. It is also acceptable for a kosher deli to require its butchers to be Jewish. However, race and color are never considered bona fide occupational qualifications.
Note that requiring candidates for a job to meet certain other qualifications is clearly an acceptable practice. But the qualifications must be demanded of all applicants; for example, a prospective employer cannot ask one candidate how fast he or she types, and then hire a candidate to whom he has not asked this question.
In addition, the qualifications must actually be necessary for the person to fulfill the requirements of the job, and they cannot discriminate against a particular group of people based on race, color, gender, etc. For example, a company hiring construction workers, who must be able to lift and carry heavy objects can require candidates to be able to lift and carry objects of that weight, but cannot require the candidates to be men as opposed to women.
Clearly the requirements of construction work would preclude someone confined to a wheelchair, but a secretarial position would not. If a job requires work on weekends, a prospective employer cannot ask a candidate if their religion or child care arrangements prohibit weekend work, but can ask if there is anything that would prohibit weekend work.
There can be a fine line between legitimate qualifications required to perform the necessary tasks of a particular job, and qualifications that only serve to discriminate against a specific group of people based on traits for which discrimination is prohibited by law. If you think there may be a chance that discrimination was involved in some aspect of your employment, contact an attorney, who will help you sort through the facts of your situation, and determine whether or not you have a case.