In the US ageism is illegal if you are discriminating against someone for being too old. It is not illegal in the US to discriminate against someone based on how young they are
As far as I know, generally, although different categories have different laws. But it's not illegal to have housing that is reserved for elderly people. It's not illegal to require someone to be 18 or 21 to come into your place of business, etc