Recording The Sound That Is Played Through The Computer Speakers

Is there a way to record any sound that the computer makes so a user clicks a button then might play a song. in my program that sound needs to be recorded and saved as a sound file (.wav or .mp3 preferably). Please help. I have never done anything with sound as major as this.

There will also be a text box that the user will enter a number in and a timer will count down from that number when that number hits zero the recording will stop.

I've got it all planned out I just need to know how to record sound that is played through the computer.