When someone is hitting you, swinging at you about to make contact, you can use similar force to prevent or stop that force. A person must basically be in the middle of applying force to you for you to be "justified" in the eyes of the law. When you have a reasonable belief that you are in danger of imminent force, you can use force to prevent it.
The law allows you to defend in situations above, but the type of force you use is also important. Similar force means that someone coming at you with his fists can be defended with your fists, but whipping out a knife is more force than necessary. Think of it as a fight between weapons; if your weapon would beat the assailant's hands down, you might be using too much force. And deadly force is ONLY appropriate when deadly force is being applied or threatened upon you. If your force is deemed "unreasonable," you could face criminal charges of your own.
When you can't...
You cannot justifiably hit someone after they are finished hitting you. One punch and walking away means that their force upon you has ended, and you can't "get them back" for hitting you. So hitting someone in the back is a dead giveaway to police and prosecutors. If you're getting taunted, and even threatened that they might hit you, unless there is an imminent threat to your person, you cannot make a preemptive strike. There are shades of gray in this legal minefield, but within a single skirmish there can be multiple times that force ends and begins again.
What can you do with this information?
Knowing how the law treats battery and self-defense is helpful, but I know how tough it is in the heat of the moment to apply those rules. Pride gets in the way, anger, and plenty of other factors, but try to keep in mind that defending yourself is not as cut and dry as it seemed in elementary school. If someone turns their back, you are taking your criminal liability into your own hands.