The Federal Constitutional Court dismissed a complaint by the far-right, anti-immigrant National Democratic Party of Germany (NPD) against the remark last August by the largely ceremonial head of state.
The fringe party had argued that the president, whose job is to serve as a kind of national moral arbiter and represent Germany abroad, is supposed to stay neutral on day-to-day party politics.
But the court gave the president a wide berth for how he performs his functions and what issues he chooses to address.
"Specific statements by the Federal President can only be objected to before the courts if the Federal President takes sides in a way that clearly neglects the integrative task of his office, and thus takes sides in an arbitrary manner," it said in a statement. "This was not the case here."
The president made the comment to students after the NPD helped organise protests against a refugee centre that had opened in eastern Berlin.
Gauck, once a Christian pro-democracy activist in communist East Germany, said: "We need citizens who rally in the streets and put these loonies in their place."
The court acknowledged that the term "loonies" could be seen as defamatory.
"Here, however, as follows from the overall style of the respondent's statements, the term "loonies", in addition to the terms "ideologues" and "fanatics", serves as a collective term for people who have not learned the lessons of history and who, unimpressed by the dreadful consequences of National Socialism, hold nationalist and anti-democratic opinions," it said.
The NPD, with around 6,000 members, scored just 1.3 percent in national elections last September and has never entered the national parliament.
However, it is represented in two eastern states' legislatures and therefore is entitled to official campaign funding under German electoral law.
And last month it gained its first seat in the European Parliament after garnering one percent of the national vote.
Germany's upper house of parliament is working on a case before the constitutional court to ban the NPD, which was founded in 1964 as a successor to the neo-fascist German Reich Party.
Chancellor Angela Merkel's spokesman has labelled the group an "anti-democratic, xenophobic, anti-Semitic, anti-constitutional party".
And the Federal Office for the Protection of the Constitution, a domestic security watchdog, has the party under observation.