The most important thing in these types of situations is to remain calm. Sometimes it'll take strength in every cell of your body to remain calm especially if your customer is at the wrong, make sure to channel your inner zen. Treat them with respect even if they're disrespectful themselves. Staying calm can help deescalate a situation.
Always approach these types of situations with utmost professionalism. If you go into a situation with hostility, you're bound to get it back in return. Sympathise with your customer and try and understand what the issue is. This coupled with staying calm, allows you to provide proper solutions to them.
Now, if you are in fact at fault don't let your ego get in the way. Avoid a scene by acknowledging your mistake and apologising for it. Most people calm down pretty quickly when they hear an apology. Once you've apologised and everyone is calmer, you'll be able to better explain how and when the situation can be fixed.
Make sure to know when to involve the manager. This is sometimes the best way to diffuse a situation. There are times where customers will be adamant and want to see the manager, in this instance get the manager because you'll never be able to get through to people who are too hard headed.
As unpredictable as a customer's attitudes may be, you always have the choice to be the bigger person and handle it with grace and dignity. Just remember, a shouting match never got anyone, anywhere.
Remember to stay calm
Approach the situation with absolute professionalism
If you're at fault, acknowledge your mistake and apologise
Get the manager involved when you know there's nothing else you can do