How to toggle LED with button on dsPIC33 and dsPICDEM MCLV-2 devboard
Try this out. make sure the S3 is the correct Macro definition for the button you are pressing, seems like you commented it wrong in the code.

while(1)
{
   uint32_t N = 21000000;
   uint32_t T = 21000000;

    if(S3 == 1) {
            LATDbits.LATD6 = 1; //make LED pin high
            while(N--); 
                        }
         else {  LATDbits.LATD6 = 0; //make LED pin low
               while(T--);  }

}


This won't check button debounce,(if this works, you can try out debounce) In some microcontroller there is Individual bits associated with the GPIO pins that can check the button debounce. If it is available,just enable it to check debounce.