A detailed explanation of MIDI is beyond the scope of this article. For a more detailed understanding of MIDI, check out Getting Started With Midi Sequencing.
Basically, MIDI allows you to record the performance, not necessarily the sound. In theory, the idea that you can record the playing first and then swap out the instrument later. MIDI data captures how hard you played the notes, how long you held the notes down, when you released the notes, etc and uses that data to trigger synths and samples.