given that it predates the era of marchandising, you can almost certainly say that people wore scarves to keep warm on cold days, and as many would make their own, making a scarf in team colours became popular.
As to when - that's harder to say. It's certainly prior to WWII though, and seems to have started early in the 20th century.