A piece of music is attributed to "Anon" if we do not know who wrote it. There are several ways this can happen.
Some music, particularly folk songs, have been handed down for centuries without being written down. Presumably someone composed them, but by the time people like Bartók, Vaughan Williams and Percy Grainger went around collecting folk songs, many attributed the tunes as "traditional". Thanks to the "Chinese Whispers" effect of passing on a tune by ear, the music had been shaped and changed with the times.
There are also written pieces that are difficult to identify. Before photocopiers existed, most music was copied by hand, making the age of the paper and handwriting not reliable indicators of age or provenance. If the title page gets lost, we can only listen to the music to see if the style is familiar. If the work is by somebody obscure, or if it isn't a good example of their work, it becomes more difficult to identify.
Copyright violation was abundant in the classical period, with many copying pieces and pretending that they had written them, or producing forgeries of the works of famous composers. As with paintings, once a piece has been identified as a fake, it can be virtually impossible to work out the composer.
There is a lot of debate about certain "anonymous" works. There are claims that "Greensleeves" was written by King Henry VIII although, having listened to some of the other things attributed to him, this seems rather unlikely. One must use a good sense of judgment and have a good musical ear to properly attribute these anonymous works to a particular composer.
Related: folk Traditional