- Regarding formal use:
It is a widely-held misconception (even among some young Japanese when they start working in a company) that you should use お疲れさま when leaving the office. Strictly speaking, this is incorrect.
お疲れさま[です/でした] is something you say to people leaving, or when you and the other person are both leaving (e.g. when you meet a colleague at the door etc.).
Saying お疲れさま[です/でした] when you are leaving, has a (very) slightly insulting overtone, in that you are essentially saying "I'm knackered and going home! [while you guys stay here]", which is not really the business Japanese way...
When you are leaving, it is probably more polite way to say:
お先に失礼します【おさきにしつれいします】: "I apologise for leaving before [you]"
Of course, there are also countless informal occasions where using any variations of お疲れ[さま][です/でした] is OK.
Mainly:
- at the end of any strenuous activity (or even not so strenuous ones, as a joke)
- as a less formal and more "matey" replacement for 乾杯【かんぱい】("cheers", when toasting).
PS: I don't think I have ever heard お疲れさん used in daily conversations: people will tend to use either お疲れさま (without です/でした → semi-formal) or just おつかれ (very informal).