Try your best to keep your cool and control your emotions. Take a deep breath or two and count to 10 to stay calm. Realize that the interaction is between the teacher and your friends. You can encourage your friends to talk to the principal or the school counselor if the teacher's yelling is affecting them.
If you yell back at your teacher it wont help the situation, but rather get you involved in a negative way and youll end up getting yourself in trouble. You can talk to your friend about different ways to respond when the teacher yells - or its probably a good idea to get the principal involved, so that if/when several people complain about the teacher, action will be taken.
In this situation you have to try to remember that, you want to help the situation and not make it worse.