The latter example is correct.
A comma follows the dialogue and comes before the closing quotation mark. A period ends the sentence. Punctuation serves to separate the spoken words from other parts of the sentence. (The Editor's Blog)
Here is the general format for commas in dialogue:
[articulation], "[dialogue]," [articulation].
"[dialogue]," [articulation], "[dialogue]."
The rule is that the commas go inside the dialogue. They can separate clauses within sentences of the dialogue, or replace periods between sentences of the dialogue. However, if the punctuation at the end of a sentence in the dialogue is a question mark or exclamation point, the comma does not replace it. The end of the entire sentence should not be a comma.