This is a really deep subject. The entire study of the craft of composition would have to be explained to give a truly concise answer.
The short answer I believe would be that there isn't a single aspect that makes an entire piece sound good. Rather, there are things that make each individual aspect sound good. Things that make them sound good together. Things that make them sound good as a part of a larger structure.
Now here's my personal philosophy that comes from observation of other composers:
I believe that most anybody who is able to put their mind to it — not all are able to, and often that's why these types don't even bother trying to become composers — can become a "good" composer if they master the many aspects of the craft.
However, there is a difference between "good" and "great" and what I believe separates the two — and although I don't think I am a "great" composer I am flattered to have been told by some that I have at least a bit of this — is that you are also an aesthete. Someone who has a keen eye, ear and imagination for the beautiful and inspiring. Even if you are writing melodic death metal or horror music.
This is something that you either have or you don't.
Every great composer and artist I have met and through history seem to have been aesthetes in addition to having a strong command of all aspects of the craft on a technical level. All of them. They all had a great sense of style and taste.
I've met composers and songwriters where it just doesn't matter how old they get. They ALWAYS know what's cool. They always have these little brilliant ideas that just pile up and pile up in their work because they intuitively know this is going to be great. It's the aesthetic choices of the piece; the choices that are actually irrelevant to whether or not the line sounds good. It's choices that are purely a matter of taste that when combined with good craftsmanship, elevates the work from sounding "good" to sounding "great".
For example, a lot of people liked my "Open Road Runaways" and "The Fortress of Your Heart" tracks. In the former, the addition of the slide guitar was a purely aesthetic choice; I had nothing in mind for the slide guitar parts, nothing written, but I just thought it would be cool and so I bought a bottle slide and improvised. Toward the end of the piece, on the last quarter-note bend before the end, I thought "Man, this part would sound sick with a phaser on just this one note". Similarly, in the latter tune, all my choices for instruments were based on what I thought would sound cool together. Wouldn't a rompler playing over a galloping bass with pedal tones be a good way to start it off? That would sound cool. Then, in the break part leading up the guitar solo, what if there was a sub-bass dive and all the other elements drop out, just leaving that reverb'd rompler to play the intro, but then there's a wicked guitar whammy dive into a solo that goes up a fourth? Man, that would sound epic and really breathe new life into it, I'll bet.
None of those choices actually impact whether the individual melody, harmony, etc. sounds good. The melody would still be a good melody if it was played on a piano, classical guitar, or a windows General MIDI synth patch — but it is these choices which take the piece to the next level and let people really connect with it. It just wouldn't be the same with something different. Although the melody would still sound good on winds, strings just feel like the choice that gives it that real "soaring" feel in the chorus. It just HAS to be strings. Without that phaser being switched on JUST on that ONE bend at the end, it just wouldn't be right. I could've harmonized ALL of the piano melody in thirds, but I chose only some of them. Because those were the notes that I just thought would sound best harmonized.
Etc.
This is the aspect of music that cannot be taught and I believe it is a necessary aspect to turn "good" musical ideas into "great" ones.