Fees serve two purposes, to pay miners for securing the transaction record and to prevent transaction flooding attacks.
If you send transactions that look like spam - frequent and small values - other nodes in the network refuse to relay them unless you include a fee, and the transaction won't even be seen by the miners.
There's a limited amount of space for transactions in each block, and miners prefer transactions that pay the most fees per kilobyte. Feeless transactions will still get mined, although it can take hours or even days if there are lots of higher-priority transactions waiting.
The wiki page
https://en.bitcoin.it/wiki/Transaction_fees has the detailed rules for calculating fees