In practice, 'smart' chargers use a special chip called a Dedicated Charge Port Controller. A DCP controller can act like different chargers, and picks the mode which seems to work best.
That's because regular ('dumb') wall chargers use USB's data connections to signal in a static, analog way what they are capable of. 'Smart' chargers can do the same, but in a less static way: they watch the behaviour of the charging device, and deduct from that the ideal setup -- which they can then emulate.
Examples of dedicated charge port controller chips are the Texas Instruments TPS2510 series 'USB Charging Port Controller', Maxim MAX14600 series 'USB Host Charger Identification/Adapter Emulator' and Norelsys NS3601 'USB Fast Charging Port Controller'.
An image from the TPS2513a datasheet (copyright TI):
An similarly, the MAX14600 (copyright Maxim):
You can see that the USB data lines ('DP' and 'DM') have several configurable resources on them, and these are controlled by some 'smart' logic.
Some explanation
Different chargeable devices require different kinds of chargers. This is partly because the USB specification did not specify 'charging' behaviour initially, and partially because some manufacturers want their devices to only charge at their quickest on their own wall plugs. Also, a device may not draw more current than what it can assume to be allowed, to avoid the risk of overheating an incorrectly designed power source.
Note: the below explanation ignores USB3, USB Power Delivery, and Qualcomm Quick Charge technologies, which complicate matters even more (e.g. they support increasing the voltage above 5 Volt).
These types of USB sockets can be distinguished:
- Regular USB port (like on a PC). This is called SDP, Standard Downstream Port. An SDP supplies up to 100 mA, and may boost that limit up to 500mA if the connected device nicely asks for it, and the USB host agrees that it's possible and allowed.
- In practice, nearly all SDPs deliver 500mA even without proper communication. USB gadgets like small fans and mug heaters don't communicate, but still require more than 100mA.
- USB port with communication capability and additonal charging functionality. This is called CDP: Charging Downstream Port. This one can supply up to 1500mA. Non-communicating downstream devices can let this know to the charger by connecting the USB data lines D+ and D- with a resistance or with a short.
- The Chinese Telecommunications Industry Standard YD/T 1591-2009 predates and inspired the CDP spec. It specifies a short between D+ and D-.
- USB port without communication capability (such as in a wall plug). This is called DCP: Dedicated Charging Port. It's basically a CDP without the means to talk to the USB host (PC).
- DCP ports which don't follow the DCP specs (from USB Battery Charging sepcification version 1.2). These wall plugs have varying setups of resistors connected to the data lines, resulting in fixed voltages which can be read by the charging device. The list of 'recognizable' voltages is very long, this is just an excerpt:
- Apple 0.5A (D+ at 2V, D- @2V)
- Apple 1A (2V / -2.8V)
- Apple 2.1A (2.7V / 2.0V)
- Apple 2.4 A (2.7V / 2.0V)
- Sony (3.3V / 3.3V)
- Many (incl. Samsung) 2A (1.2V / 1.2V)
'Smart' chargers -- i.e. the 'DCP controller chips inside them -- can emulate a standard DCP port with varying resistor values, and can also emulate several of the non-standard DCP voltages.
The secret sauce
The chip manufacturers don't really describe how their 'detection' logic works, but the only option they have is to monitor what the charging device is doing on the D+ and D- lines, and to look at the current that is being drawn.
This no doubt requires a lot of trial-and-error research, and the algorithm is probably kept as a closely guarded secret.
One technique which might be used, is to cycle through the emulated states and noting the amount of current that is drawn. The state with the highest current draw then is the 'best' and remains active.
Side note
Don't be confused by the marketing of the charger manufacturers which claims that a chargers "delivers the maximum current for the device". A USB socket is a voltage source; it only controls the voltage and it's the charging device which 'decides' how many current it 'takes in' while charging.
So there is no way a charger can 'force' a current into a device that does not accept it, at least not by staying below the safe maximum 5.1 Volt.
The only thing going on is some trickery to make the charging device believe that it's connected to the most capable, best-suitable, most compatible charger it can imagine.
Further links