Arguably the most prominent aspect of what shapes the mood of a particular piece of music is the key. A majority of the keys used for every type of music, from classical to rock, can be divided into major keys and minor keys. Major keys are considered more uplifting or "happy," while minors key are considered more lethargic or "sad." Although it's hard to so nicely package the range of emotions created by each key, simply put, the notes in a major key scale flow between each other with far less dissonance and tension than their minor key counterparts. This is the explanation for one seeming happier than the other, and is the first step to accurately capturing a mood in a musical composition.
The timbre is another aspect of music that has powerful affect over the mood. Also referred to as the "color," the timbre has nothing to do with what note is played, but with the shape and tonality of the music. For instance, the exact same middle C note played on an electric guitar will have a completely different character than that played on a piano. This has to do with the materials used to create the instruments, such as wood or metal, along with how they are played, such as strumming or blowing. Timbre is most often considered in classical music, which employs an extensive range of instruments, and it's up to the composer to choose which timbre fits the appropriate mood of the musical piece.
The speed of music is perhaps the simplest aspect to grasp for those with no musical training. Like a bicycle gently rolling down the road or a freight train barreling at a hundred miles an hour, the difference that speed can make in a piece of music is quite blatant. What's interesting, though, is that there is no speed for happy or sad, it's simply a matter of intensity. A slow tempo song can work in the context of a peaceful, daydreaming mood or for a somber, depressing mood. In the same way, a fast tempo can create the feeling of giddiness and excitement, as well as stress and terror.
Rhythm is considered the most primal aspect of music. In fact, the ultimate metronome is the human heart. As humans, we have the urge to move to a consistent beat, and when those beats are thrown off, it usually results in the disruption of the entire bodily system. With music, of course, it's easier to apply shifts in rhythm without catastrophic results, but those shifts are what evoke certain moods. For instance, the 3/4 tempo, most often heard in the waltz, is associated with a feeling of floating while the 2/4 tempo, used in bluegrass, is associated with that rushed, upbeat feeling. If the rhythm is too complex, the result is often jarring, resulting in confusion and nervousness. All of these rhythmic ideas, when applied properly, can greatly enhance the effect of the mood on the listener.