You asked this question:
Hi. I am confused about how to say 'How are you?' in a formal way. Can you help me? Thanks.
My answer:
There is no need to be confused. In fact, 'How are you?' is fine for any context, including formal situations.
While there are many other ways of saying this for informal situations, 'How are you?' is the only way to ask about someone's health in a more formal situation. For example, if your boss at work has been unwell, you could ask 'How are you?' and it would be perfectly polite and appropriate.
NB The phrase 'How do you do?' exists, but it does not mean 'How are you?'. It is a rather outdated formula used in formal contexts mean 'I'm pleased to meet you'. Do not try to use it in other situations.
I hope that helps.