To make it simple:
Mining consist on finding a specific block that once hashed gives a value bellow a certain threshold (the target).
The target is recalculated every fixed amount of blocks (2016), so that it is refreshed every two weeks (at 1 block per 10 minutes rate).
When recalculated, the total network hash rate is taken into account, so that according to that value a block will be mined every ten minutes on average. Notice that this is purely probabilistic.
How does it work?
The target is a 256-bit value that is reset periodically. When a block is mined, the SHA-256 hash is performed over the block and the result is compared with the target.
Now, if the target is a huge 256-bit number, the hash of the block will be easily smaller than the target, and the block will be considered valid. However, if the target value is really small, finding a valid block will be way harder. Here is where the network hashrate comes into play. The bigger the hashrate, the lower the target. So, once 2016 block have been published since the last update the target will be recalculated, making it higher or lower depending on how the total network hashrate has evolved.
Notice that it could happen indeed that a block is found in less, or more, than the 10 minutes, but the odds are more likely to be around the mark.
How is it calculated?
If you want to know exactly how it is calculated, here you have a Python code that does so:
max_tgt = int("00000000FFFF0000000000000000000000000000000000000000000000000000", 16) # Around 2^224
current_diff = 440779902287
current_tgt = max_tgt / current_diff # current_diff = max_tgt/current_tgt
hash_rate = 3310469378 * pow(10, 9) # GHash/s to Hash/s
print current_tgt/float(pow(2, 256)) * hash_rate * 600
By dividing the current target by the maximum number of possible hashes (2^256) you get the odds of finding a valid hash. If you multiply this by the hash rate you get how many correct hashes will be get in a second. Finally, by multiplying it by 10 minutes (600 seconds) you get how many valid hashes, on average, you will get in ten minutes.
What gives you:
Data such as maximum target (
max_tgt), current difficulty (
current_diff) and hash rate (
hash_rate) has been obtained from bitcoinwisdom, and matches with the current network data. There you can also find some charts about how the hashrate and the difficulty has evolved in the last months.
For those wondering how the network hashrate is actually computed, you can check it in this answer.