Note: I only mention iTunes/Amazon MP3 because they are the largest distributors of digital music for purchase. I don't refer to others distributors because whatever they offer will likely be available in either, or both, iTunes or Amazon MP3.
If you live in Japan, then you can find pretty much any anime soundtrack OST/opening/ending/insert song in the Amazon MP3 JAPAN store or if not there, the iTunes JAPAN store for purchase.
Note that I emphasize the JAPAN; you will likely NOT be able to find what you're looking for in an Amazon MP3 or iTunes store for a different country.
Although it pertains to the United States in particular, this link more or less explains the gist of why that is so.
However, there is a small amount of anime music you can find in a non-Japan iTunes/Amazon MP3 store. Here are couple examples that I have found in either, or both the U.S. Amazon MP3/iTunes Stores (but the trend seems to be that there is mainly J-Pop opening/ending songs, and very little instrumental OSTs):
- Yui's "Again" from Fullmetal Alchemist Brotherhood
- LiSA's "Crossing Field", "Shirushi", Eir Aoi's "Innocence", "Ignite" and a couple of other opening/ending songs from Sword Art Online 1 / 2
- Most of Fairy Tail's opening/ending songs (NOT the OST though, unfortunately)
- Hiroyuki Sawano's OST for Attack on Titan (nothing for Aldnoah Zero though) and Linked Horizon's "Guren no Yumiya" and "Jiyuu no Tsubasa"
- Joe Hisaishi's complete OSTs for a couple of Miyazaki films including Spirited Away, Castle in the Sky and Princess Mononoke (iTunes only)
If you don't live in Japan, as the above link alludes to, then it is very difficult to truly legally obtain anime music via download.
Other than legally purchasing from the small library of anime songs available in your own country digitally from iTunes/Amazon MP3, (I know this defeats the purpose of your question of digitally obtaining anime music, but) unfortunately the only other option is to purchase physical CD's from various vendors like Amazon, CDJapan or some other CD distributor.
I will leave this link for you to read. It tells you ways to obtain Japanese music outside of Japan both physically and digitally.
You will notice that there is a trick involving buying Japanese gift cards from a middle-man like Japan Codes and then creating an iTunes Japan or Amazon Japan account, BOTH OF WHICH ARE ILLEGAL if you don't live in Japan because they require that you have a valid address in Japan.
I mention the gift cards because anybody looking into purchasing music from digital stores from countries that they don't live in will likely consider buying foreign gift cards, but since the question is about LEGAL methods, I wanted to make the point that it is technically illegal to do so if you don't live in the country that the gift card in question is for.