I took a class on the meaning of music and this was one of the things we discussed. On the whole, we were exploring the idea of whether music had any inherent meaning or if the commonly accepted meanings were socially constructed. It seemed in the end that this is really an opinion since many people over the years have argued both ways. I personally came to believe that music does not have inherent meaning, except for a few possible exceptions. One exception being how we refer to pitch, being high vs low. Strong, low frequencies tend to resonate through the body and can be felt through the ground, given enough amplitude. Higher pitch frequencies are more so heard than felt. So since we have ears on our head, the highest part of the body, and can feel low frequencies below that, it's easy to suggest that our convention of referring to them as low and high are related to where they stimulate us vertically. However, we could still argue that this is a social construct since the sound isn't actually propagating any differently based on how high we're able to feel it; it's really just how those sound waves are capable of interacting with our body, where high frequencies are not as able to penetrate as the low ones and are more likely to bounce off instead of vibrating our chests.
So the issue at hand when we discuss the meaning of music is that we are not able to use music to explicitly refer to an object (without the use of lyrics). For instance, we're not able to say musically, "this is a tree", or even just "tree". Music is arguably the most abstract of the arts for this reason.
Beyond that, when we approach things more abstractly, we are typically referring to these different keys as having some sort of emotional meaning and emotions are subjective. Everyone experiences happy and sad and mad and lonely differently. It is commonly accepted that Major is happy and Minor is sad but it's also easy enough to find examples of Major keys used for sad songs and Minor for happy ones. Major/Minor only gets more complicated when you add 7ths, where a Minor 7 tends to feel less sad to the average person and a Major 7 tends to feel less happy. I think that the abstract nature of music is one of the reasons that it can be so useful for expressing emotion; it allows the listener to interpret the emotional content from their own perspective without having to look past the explicit statements. However, this is also why it is entirely subjective, making it quite difficult to nail down a particular key or tonality as having a defined meaning.
There have been a lot of social constructs created over the years; far too many to cover in this answer (however long winded I tend to be). For instance, horns have been used to represent heroes quite often. This can be attributed to the use of a horn to signal the return of the soldiers from war, or the use of the horn to signal the army to attack. As others have mentioned, before valves, horns were designed for specific keys due to their limitations in producing some intervals from the fundamental frequency. So the more commonly used keys for heroes tend to be the keys that are best accommodated by the most common horns, which are generally the "flat keys" (those with flats in the key signature).
For a somewhat less academic approach to the subject, I recommend watching The Lion King and listening to the sound track. You'll find examples of instruments having a specific meaning, such as horns for the hero, strings for tension and sadness, as well as scales having a meaning, such as happy songs being Major and, one that I find particularly interesting, "exotic" tonalities/scales being used to represent the bad guy, like harmonic minor (I find this particularly interesting because this construct seems to be based on such scales having a foreign sound, which can be used to represent bad things, most likely due to xenophobia; it's also often used to represent the "evil seductress" but I don't think there is one in The Lion King).
So I'd say that the meanings that composers have for certain keys are based on social constructs. Some of these are based on the instruments themselves, others on cultural aspects, and some of it is just based on the idea that we are using an abstract medium to convey an abstract experience (emotion), so people tend to feel something when they write and happen to choose a given key at the time. There's a lot of content out there if you're looking to study this more academically but I can't remember any particular book or paper that I could suggest off hand, unfortunately.