You have to use it as a subject in every sentence.
Dzień dobry pana.
Dzień dobry pan
u (Dative case after "dzień dobry", I could explain why, but not sure if you need this knowledge right now)
or
Witam pan
a (Accusative case after "witam")
Jak się masz?
that's a strange mix, "jak się masz" is colloquial
per se, so it doesn't go well with formal speech, but I think you can use a similar verb (miewać się which is the imperfective version of mieć się, and it sounds less colloquial), then:
Jak się pan miewa?After Pan/Pani you always use the 3rd person singular, after Państwo - the 3rd person plural.