how do you know when you have to add a comma to a subordinate clause

Generally, if the subordinate clause is first in the sentence, it needs a comma after it (before the main clause begins).

If the subordinate clause is somewhere in the middle of a sentence, it needs commas only if it's a non-essential clause.

If the subordinate clause is adverbial and at the end of the sentence, no comma is needed. If the subordinate clause is adjectival and non-essential, there needs to be a comma before it.