Inside the slot machine is a microprocessor similar to the one in your home computer. Instead of running Word or Excel, it runs a special program, the RNG, that generates numbers to correspond to the symbols on the reel of the slot machine.
You might say that the RNG is in perpetual motion. As long as there is power to the machine it is constantly selecting random numbers every millisecond. The RNG generates a value between 0 and 4 billion (approx number) which is then translated into a specific set of numbers to correspond to the symbols on the reels. The outcome of each spin is determined by the number selected by the RNG. This number is chosen when you hit the spin button or deposit a coin.
The RNG uses a formula known as an algorithm which is a series of instructions for generating the numbers. The scope of this is beyond most of our mathematical knowledge but can be checked for accuracy. This is done by the Casino Control Board and other testing laboratories to make sure that the program performs as it should so the player will not be cheated.