Jazz musicians used the word "cool" in the 1940s to describe a style of jazz that was more subtle and restrained than styles such as bebop. Miles Davis is among the most noteworthy of musicians to popularize the cool jazz style. The jazzmen may have been reacting to the impresarios of an earlier era, who designated their music "hot." They quickly began to apply the term elsewhere, as a signifier of approval.
However, the selection of this term was likely influenced by an even older popular use of the word to mean, rational, restrained, or level-headed, as in Calvin Coolidge's 1920s presidential campaign slogan, "Be Cool with Coolidge!" This other use of "cool" goes back to the 1840s, at least, and may be older yet. The related term, "cool cat" existed at least since the 1920s. "Cool" itself was prominent in Black culture by the 1930s, and appears in written text from that era with its current meaning. The word's popularity may have come from an African-American culture that prized a detached attitude as a way of avoiding scrutiny. Its use had become widespread by the 1950s. It was further popularized by the beatnik and hippy subcultures, or at least by the marketing thereof. As previous write-ups indicate, it remains current.
A now-deleted write-up attributed the word to Stephen Sondheim, who allegedly claims to have first used the word "cool" to mean "that which is positive" in the musical West Side Story. I cannot verify whether or not he made that claim, but, living in the musical culture of the mid-twentieth century, it would be almost impossible for him not to have heard it prior to using it in that musical.