It is often said when someone accuses you of doing something that you haven't done
James: You stole my bank card!
Ruth: No, I didn't. Why would I do that? (it won't work for me because I dont know what your pin code is)
It asks for the hypothetical reasons for doing something - that what could've led him to do that
Furthermore, by saying this, the person is making a statement that it is so unlikely of him to do that
But what I don't understand is why do people say "Why would I do that" instead of "Why would I have done that?". (as the action was done in the past)