The most simple hot water boiler is a kettle on an open flame, but that's not very effective if you need lots of hot water. Boilers work by transferring the heat from a fire into the water it contains. In most boilers the transfer is achieved by conduction.
A Better Way to More Hot Water
To make the best use of all the heat that is available, you need a large surface area on the boiler. Gas hot water boilers have a system of tubes called a heat exchanger. The water is driven through it by a pump. These tubes are, in reality, one very long tube which turns back on itself over and over again in what looks like a stack of shorter tubes. This exposes a greater surface area to the gas flame, so more heat can be absorbed by the water.
This heat exchanger is extremely efficient. Many boilers are able to deliver a constant stream of heated water. This eliminates the need for a hot water tank.
Oil fired boilers work in the same way.
In an electric boiler, the water is heated by an element inside a large tank. Once the tank is full of hot water, it can be drawn off. Cold water enters at the bottom of the tank. This drives the hot water out through a pipe at the top of the boiler. These boilers can only heat up one tank of water at a time.
Hot water boilers are also used to drive machinery. The boiler in a steam locomotive is little different from your domestic boiler – it is just bigger.