could you tell me when, in a formal letter, do we use To whom it may concern rather than Dear Sir/Madam
I would always write "Dear Sir or Madam". (Or perhaps omit the salutation altogether.)
"To whom it may concern" is old-fashioned and probably arrogant. It makes sense only in an open letter--an introduction or a general permit--that is shown to as many people as necessary.