Recently, filmmaker James Gunn explained on Twitter that Groot's language was such that you need to spend time with the character to make sense of his 'I am Groot' utterances, which is how the rest of the Guardians of the Galaxy can understand him. But this theory contradicts the scene in Avengers: Infinity War, where Thor reveals he can understand Groot because he took his language as an elective course on Asgard. Gunn explained the truth of Thor's explanation.
"Thor was making a joke, as he is prone to do, when he said Groot was an elective. Being a god of sorts he doesn't have the same high bar for understanding Groot as the Guardians do.
That is certainly one way of interpreting the scene. That Thor's godly powers allow him to instinctively understand alien languages, and the 'elective course' bit was just a joke. Thor: Ragnarook has successfully proven that the God of Thunder has a well-developed sense of humor. He spends large parts of the film poking fun at others and himself, so it is conceivable that he would carry over that humor into his next film, where he met the Guardians and spoke to Groot.
However, you have to consider Thor's state of mind when he made the comment. The God of Thunder had just been rescued from certain death, and he had barely managed to escape an attack on his life by Thanos, which had also resulted in the decimation of a large portion of the remaining Asgardians, including Loki and Heimdall. It is unlikely that the character would be in a joking mood at that moment, while he plots a way to kill Thanos with aid from the Guardians.
Of course, the most likely explanation is that Gunn's backstory for how others can understand Groot's language was not communicated to the writers of Avengers: Infinity War, who then included the scene between Thor and Groot without realizing it contradicts the lore of the Guardians of the Galaxy as established by Gunn.
At the end of the day, such minor plotholes are inevitable when multiple writers are working on different parts of the same franchise. However Thor learned to communicate with Groot, their alliance proved to be a game-changer. It was Groot, along with Rocket Raccoon, who accompanied Thor on his quest to find a new weapon capable of killing Thanos, and it was Groot who sacrificed a limb to make a handle for Stormbreaker, the ax that the God of Thunder used to behead present-day Thanos in Avengers: Endgame.
Thor and Groot will likely reunite on the big screen for the third time in the next Guardians of the Galaxy feature, which Gunn is helming. The story will follow the Guardians with their guest Thor tracking down the Gamora from the past who is wandering around the galaxy in the present times. Then the God of Thunder will be off on his own adventure in Thor: Love and Thunder, where he meets a new Jane Foster now imbued with godly powers and wielding his enchanted hammer Mjolnir.