Apple's keyboard has a set of media controls to navigate the music playing on iTunes, allowing you to play, pause, skip, and replay songs and videos. Unfortunately, these controls are exclusive to iTunes; if you're playing music from Spotify, Pandora, or the myriad of other online web-based services (Amazon, Rdio, SoundCloud, etc.), you're out of luck.
However, with the help of BeardedSpice, a menu bar app for Mac OS X from developers Jose Falcon and Tyler Rhodes, you can lift those restrictions to control web-based media players in Chrome and Safari with the media controls on your keyboard.
First, download the BeardedSpice .tar file and unpack it, then drag the BeardedSpice app into your Applications folder. Next, open up the app; just make sure that you have allowed third-party app installs in your Mac's security settings, if not done already.
After successfully opening it, an icon will appear in your menu bar showing all of the webpages that are compatible with BeardedSpice, including Spotify (web player), SoundCloud, Pandora, YouTube, Google Music, and many more.
Select a web player to control from the menu bar list, as you can only control one at a time. From there, you'll be able to use the media keys found on at the top of Apple keyboards to control the music: back, play/pause, and next.
In the "Preferences" menu, you can set shortcuts for things like setting the active tab and favorites, and you can also enable/disable keyboard controls for specific sites.