Op I can see the point you were trying to make. It was not that he was told off that upset you, but how it was done.
It is a completely normal parental instinct to be upset when you witness someone shouting at your child. Any adult, especially a teacher, literally shouting in a child's face, and calling them 'stupid' an 'idiot' or any other form of name calling is completely unacceptable.
You have not failed him in any way. You removed him from the situation at the time. You did the sensible thing by not reacting in an overly emotional way. Walking away from confrontations when your children are with you, is the best approach. I admire you for holding your composure.
From how you describe the incident, the teacher displayed to everyone that she cannot control her temper or articulate herself in an appropriate manor. Screaming in the face of a young child is intimidating, unprofessional and immature. An adult calling a young child 'stupid' or an 'idiot' is never justifiable. Your son may well have needed to be told off for climbing on the tree, but she handled it poorly and resorting to name calling is ridiculous, bullying, behaviour.
I think it is completely reasonable to complain in writing to the HT to let them know that you found this teacher's behaviour intimidating and unacceptable and to request a follow-up meeting to discuss your complaint. You need to find out if there were witnesses and ask them to complain as well. This teacher lost her temper/self control in front of all the children and was setting a terrible example of how to behave in school / deal with disagreements. It's tricky, as I doubt you can expect an apology, directly to your son, but I don't see how any school could justify calling a child stupid or an idiot, within any discipline policy. The outcome of the complaint that I would be looking for would be a meeting with the HT & teacher in question to discuss what forms of discipline I could expect while my child was a pupil at the school. I would also want some reassurance that the school does not accept teachers calling pupils names, even if they weren't willing to admit that it actually happened on this occasion.
I would let your DS know that he mustn't climb the tree again and that if he misbehaves at school he will be told off, with your permission. I would praise him for discussing how it made him feel, with you. I would also reassure him that he shouldn't feel embarrassed or scared, because everyone gets told off or shouted at, at some point. Ask him to think of times when his friends have been told off before, but they were fine afterwards. However, I would also let him know that, while the teacher was right to tell him off, she was wrong to call him names, and that you have told the HT.