TL;DR: Out of universe, the lyrics are from the books, and the music is by Ramin Djawadi.
This is part of my answer to a related question on another Stack Exchange; this was based on this article detailing "the origin story behind the most iconic song on Game of Thrones":
"Rains of Castamere" is the most well-known piece of music in George R.R. Martin's "A Song of Ice and Fire" universe. The song tells the story of how House Lannister destroyed the rival House Reyne of Castamere when they dared to rebel. In the years that followed, "Rains of Castamere" would play to both celebrate House Lannister and strike fear in the hearts of their enemies. The tune was famously played at the beginning of the Red Wedding, signaling the Freys alliance with the Lannisters and cuing the massacre's start.
Given the rich history of the song, its onscreen debut in "Game of Thrones" was a widely anticipated moment for fans of the book series. But there was a catch: No one knew what the actual melody of the song would sound like.
"Benioff and Weiss said there's lyrics in the books for 'The Rains of Castamere' that we would like you to write a melody for," Djawadi tells INSIDER. "It needed to be haunting but beautiful, and all these other criteria because the theme had to be really flexible. So just based on the lyrics I sat down and wrote it."