I shall try to explain as simply as I can and as much as I understand. I am not an expert but I shall try.

Nodes (Miners) solve bitcoin puzzles by guessing at random. The hash function makesoutput prediction impossible. So, miners guess the mystery number and apply the hash function to the combination of that guessed number and the data in the block. The resulting hash has to begin with a pre-established number of zeroes. There's no way of knowing which number will work, because two consecutive integers will give wildly varying results. What's more, there may be several nodes that produce the desired result, or there may be none (in which case the miners keep trying, but with a different block configuration).

The first miner to get a resulting hash within the desired range announces its victory to the rest of the network. All the other miners immediately stop work on that block and start trying to figure out the solution for the next one. The victorious miner gets some new bitcoin as a reward for its work.

The difficulty of the calculation (the required number of zeroes at the beginning of the hash string) is adjusted frequently, so that it takes on average about 10 as per decided by the bitcoin devs.

This is why miners will cost them money to buy mining tools which can mine faster than the other and will take the victory in which they mined it first. I don't know what algorithm they will use as there are lots of algorithm that can be use for mining like SCRYPT MINER for altcoins like LITE COIN.