How is the ability of a single charger to charge a range of types of mobile phones achieved?
The thing you are missing is that the external "charger" is not a charger at all; it's just a DC power supply, providing 5V at some maximum current, typically 1A or 2.1A.

The actual charger is circuitry built into the phone itself. It takes the incoming 5VDC and controls the voltage/current going into the battery. Because it is part of the phone itself, it is already programmed with the characteristics of the battery. (In some cases with removable batteries, the battery characteristics are stored inside the battery pack, probably in a small microcontroller, which communicates with the main charger.)

There is another factor, which is the external power supply indicating to the phone how much current can be drawn. This is different between Apple devices and Android devices and may involve fixed resistors on the USB data lines or a small microcontroller inside the power supply brick.