Membership sets to Cancelled when Contribution Fails. Why?
This is the expected behavior in CiviCRM, when you have a Contribution related to a Membership, and you set its Contribution status as Cancelled (manually or automatically through a ipn payment processor) it will cancel the Membership too.
You can take a look to source code (4.6.x) where this is happening

https://github.com/civicrm/civicrm-core/blob/4.6/CRM/Contribute/BAO/Contribution.php#L1512

You can patch civicrm core to avoid Membership to be Cancelled when Contribution around this line:

https://github.com/civicrm/civicrm-core/blob/4.6/CRM/Contribute/BAO/Contribution.php#L1484

if (!$baseIPN-&gt;validateData($input, $ids, $objects, FALSE)) {
  CRM_Core_Error::fatal();
}

// ## Patch Begin ## Fix to not update membership status when updating contribution status
$memberships   = array(); //&amp;$objects['membership'];
// ## Patch End ##
$participant   = &amp;$objects['participant'];
$pledgePayment = &amp;$objects['pledge_payment'];


There is a forum thread discussing this:

http://forum.civicrm.org/index.php/topic,34082.msg154608.html#msg154608