Here's all the reasons I know of for composers to pick a certain key.
Physical limitations
Some instruments have/had physical limitations. For example, certain harps cannot play all keys and antique horns and trumpets only had certain pipe fittings they could use that tended to be tuned toward the flat side of the circle of fifths (Bb, Eb, etc). Triangles are only available tuned to certain notes, etc.
Considerations for stringed instruments
Guitarists who use traditional tuning will love a piece written in E because they will be able to use many open chords.
Hendrix and Cobain notedly tuned themselves one half step flat so they could play pieces in Eb. Punk music commonly uses drop-D tuning which bottoms out at D natural.
The same sorts of constraints apply to other stringed instruments such as the violin, where the composer may have chosen A, E, D, or G to allow the use of open strings or certain double stops.
Form
Some works are part of a larger form, e.g. a movement in a sonata. The first and last movements will typically be in the same key and inner movements will be in a prominent secondary key area, usually V in classical and V or VI in romantic music.
Tesatura for a vocalist
Some works are intended for a specific vocalist and either challenge their upper and lower ranges or linger around certain notes in the vocalists' "sweet spot." The range and therefore the key was selected for optimum performance.
Symbolic meaning
In the old days before equal temperament, not all keys sounded the same, and took on a certain character and meaning by convention. Some examples:
- Mozart used the key of D major to represent magic and the divine supernatural
- Wagner used the key of F# to represent disturbed emotional states
- The key of F major is associated with hunting horns and the outdoors, and is frequently used in pastorales
Ease of play
A key with a lot of black notes is a lot easier for a pianist to play-- there is more tactile variety in the fingers which allows them to know where the keys are without looking.
Horn players generally prefer songs written to the flat side, e.g. Bb and Eb, as they are easier to play in tune.
Clarity of notation
Sometimes a key will be chosen because it is easier to notate. F major is a lot easier to write in than F# major.
Some twentieth-century composers got in the habit of writing everything in the key of C and using only accidentals so that the performer would not have to remember the default sharps and flats at any given moment, which allows the composer to switch keys more frequently without confusing the performer too much.
Consonance with samples
EDM and other modern genres use a lot of sampling (borrowing waveforms from other songs) and choose the key so that the surrounding material is consonant relative to the key of the sample.
To accompany other sounds
Opposite of the above; music composed for sound tracks may be put into a certain key to accompany the rest of the sound going on at the moment in the scene. For example, in The Hobbit, parts of the sound track accompany dwarven signing, or in in the 1979 film Tess, Sarde's sound track accompanies a wedding party.
Color and sound
Lower keys just sound, well, lower. Sometimes the overall color and sound of the work is the most important.
As an exercise
Sort of an edge case but listed here for completeness. Some works, most notably the 12 fugues and preludes of Bach's Well-Tempered Clavier, attempt to use certain keys to complete an exercise, i.e. the composer has an opportunity to try is hand at every key, the performer has an opportunity to play in them, and the listener has a chance to hear them (of particular interest when the instrument is not equally tempered).