Heparin is an anticoagulant - it reduces or stops the ability of blood to coagulate into a thrombus or clot.
The way it works is by upregulating the activity of one of the body's natural anticoagulant molecules. This molecule is known as antithrombin III (AT-III), and heparin binds to it, thereby increasing its efficacy by up to 2000 times. And so, with the body's anticoagulant system boosted so greatly, coagulation is correspondingly reduced.
That's the simple answer; if you'd like a little more detail, keep reading. When heparin binds to antithrombin III, the complex inactivates a number of coagulation enzymes, but the main two are thrombin and Factor Xa. However, heparin molecules vary greatly in size, and only the larger ones can inhibit thrombin. This means that the effects of heparin are quite variable, since they depend on the amount of 'large heparin molecules' in the mixture you are administering.
Enter low molecular weight heparin (LMWH), which is just the 'smaller heparin molecules'. Although LMWHs will therefore only really have anti-factor Xa activity, this is actually an advantage in many settings, since the anticoagulation response will be much more predictable (monitoring is usually not required). LMWHs also seem to have a better side-effect profile than the 'unfractionated heparin' above, but are more expensive.