Can anyone tell me what purpose is served by classifying nouns as masculine, feminine or neuter? Why does German have three genders when the closely related English has no gender system apart from pronouns and most other languages in 'Western Europe (all modern Romance and Celtic languages) have two genders.
German inherited its three-gender system from Proto-Indoeuropean, but the reason that language had it is lost in the mists of time. Some languages kept this. Others reduced to two genders (e.g. French before the Normans invaded Britain in 1066). English still had three genders up to that point. Once the two languages started to merge they gave up the Germanic three-gender system and the French two-gender system pretty quickly. It is easy to see why. The English speakers would not know the Norman French genders or inflections and vice versa. So we ended up with a massively levelled system with one French infection (the -s plural) and one German (the -s genitive, from masc/neut sing, one noun class) and gave up all inflections on adjectives and articles.
So whilst the strict answer to why German should have the three-gender system is ultimately "why not?", the answer to the question of why German and English have different gender systems is that it was English that changed, not German, after we were invaded by the Normans in 1066.
As is true with all languages all through time, linguistic features are not extant in order to 'serve a purpose', but rather often as remnants of things that once served purposes, then became obsolete.
One good point to direct you in further research of the history of language development: https://linguistlist.org/issues/5/5-525.html
And one good read on a related topic (the history of linguistic genders): https://www.jstor.org/stable/pdf/27699089.pdf
There is no purpose.
This is the direct answer to your question. The slightly deeper problem of your question is the assumption that grammar would aim to achieve a purpose. That's just not the case. Natural languages are not designed, but evolved historically. It is possible to have a language with gender and to have a language without gender. German just so happens to be a language with gender.
Yet another question could ask for the consequences of that, i.e. for instance the social consequences, on behalf of style, on constraints this is imposing on other grammatical phenomenons. But this is a very broad question which is to broad for me to answer.
3 genders can be useful for formulating "German-style" sentences
While calling it the purpose of gender would be too much, one advantage of 3 grammatical genders is that in many cases, confusion with personal pronouns or relative clauses can be avoided.
Im Haus stand ein Tisch. Er war schwarz und es war blau.
While this is not the best example, it is clear that the house is blue and the table is black and not the other way round.
Ich traf einmal eine Maus in einem Wald, der sehr alt war.
Ich traf einmal eine Maus in einem Wald, die sehr alt war.
Here, the second version is what I call "German style" in the sense that you could not achieve this word order in English.
In gendered languages, grammatical genders are useful to label the gender of things that naturally come on two genders, and a lot of languages have a third gender for things that don't have a gender. With time and language evolution genders may shift and other ways to express gender may appear (like the suffix "in" in German) and in the end grammatical gender can became arbitrary and not very useful - or as Megan Holloran's answer point, became a remnant of a things that once served a purpose.
To see how genders are useful, I'd suggest taking a look to romance languages, where they still serve a purpose, although there a lot of words without a natural gender but have an arbitrary one and may give a hard time to language learners.