EEOC: It is illegal to publish a job advertisement that shows a preference for or discourages someone from applying for a job because of race, color, religion, sex, national origin, age, or disability. For example, a help-wanted ad that seeks "recent college graduates" may discourage people over 40 from applying and may violate the law.
One exception is BFOQ (bona fide occupational qualification). Some obvious examples are labor jobs, religious education and the performing arts. There are gray areas as well, such as a college-oriented start up hiring recent college grads, however the company would need to prove that the age requirement was necessary to perform the job in question. Personally, I think it's rather hard to make that case for programmers.
Interestingly, a preference for minorities is not a BFOQ. It is illegal to specify race or gender even if end goal is workplace diversity.