What function does the comma serve in the salutation of a letter, and when did it come about?

Using a comma before or after naming the person who your speech is directed at, just indicates that direction. Like:

Hey, John, how’s it going?

It is used to direct speech and guide the reader to understand who the message is intended for.

I imagine it was always used.

The use of white space— beginning the body of the letter on a separate line— may simply be to make it easier to make out the greeting from the body, and to create a nicer appearance easier on the eye.