It's impossible to tell all the circumstances in which to use these sentences. There are too many situations!
If you observe that someone has done something and you believe that in their place you would have been equally successful, you can say "I could have done that". You did not do it, but you could have done it. You had the ability to do it.