The general rule is, If a quote appears in the middle of a sentence, change any final period on the quote to a comma. If the quote ends with a question mark or exclamation point, leave this symbol intact. Do not add a comma. So for example:
"Give me the box," John said.
"Give me the box!" John screamed.
"Will you give me the box?" John asked.
(See Modern Language Association Handbook, section 3.7.7 in the 6th edition)
If the exclamation or question mark is not part of the quote, put it outside the quotation marks.
Why did John say, "Give me the box"?
Tangential note: When you need a comma or a period after a quote, the "American style" is to put it inside the quote marks, while the "British style" is to put it outside the quote marks.
Even though I am an American, I think the British style is more easily readable and clearer. For example:
American: Today we learned the words "apple," "pear," "orange," and "grape."
British: Today we learned the words "apple", "pear", "orange", and "grape".
The British just seems clearer and more easily readable to me.
Also, the British avoids ambiguity. This came home to me once when I was writing instructions on how to enter numbers into a computer system. I wrote that the user should not enter any decimal point following a number. Then I gave as an example:
Don't enter "14." Instead enter "14."
Well, this is not at all clear in American style. But in British style it makes perfect sense -- subtle maybe, but at least the reader could figure it out:
Don't enter "14.". instead enter "14".