The boy was expelled by the head teacher at Bonus Pastor School in Bromley, Kent, after allegedly repeated disruptive behaviour.
The boy, identified only as "student P", went to the High Court in London, arguing that teachers did not have the contractual right to refuse to teach individual pupils in this way.
But ruling that the union's action was lawful, Mr Justice Morison said that it was a "tragedy" that the difficulties in the boy's education should have reached the courts.
"There must be a better way, given reasonable goodwill, for the problems to be overcome.
"I trust the parties will now find it possible to turn over a new leaf. 'P' is at an important moment of his educational life," the judge said.
However, disruptive pupils were a menace and damaged the educational interests of other children in the class, he added.
'Unteachable'
Staff had claimed the boy, who denied the allegations, had threatened teachers, assaulted pupils and was "unteachable" in the classroom.
The ruling against "student P" means members of the NASUWT will be able to continue with their refusal to teach him.
The union's general secretary, Nigel de Gruchy, said: "We have secured an important victory in as far as we've established the right of teachers to take action to defend themselves against violence and disruption in the classroom".
The inclusion of such pupils in mainstream classes "wrecked" the education of other pupils and made the lives of teachers "absolutely impossible", Mr de Gruchy said.
"The industrial action is - and has been decided in the High Court today - a legitimate trade dispute, and that was a very, very important legal principle which is at stake.
"And obviously I'm very relieved we managed to defend that position successfully," he said.
The NASUWT currently had 33 similar cases on its books and would continue to defend the rights of teachers, Mr de Gruchy added.
'Seek justice'
Neither the pupil, nor his family, was in court to hear the judge dismiss the action.
"Faced with adversity, a parent has to do what's right within the law to seek justice for their young," the statement said.
She had always ensured that she worked in partnership with the school in matters concerning her son's schooling, she added.
It is understood the pupil's lawyers are considering an appeal.
The youngster is currently being taught in isolation at the school by supply teachers - which his mother said was denying him a proper education.
The NASUWT had argued the pupil's education had not suffered, because he now had the benefit of one-to-one teaching.