Ok so I know this is answered already but not well. I just finished designing a charger for a wearable that does this so I'll try to explain. Engineers using USB for charging are well aware that they will encounter sources of varying quality, capability and spec. The USB standard for one provides several options. You have your old school start at 100mA raisable to 500mA standard USB port. You have dedicated battery charging ports 3.25A, charging downstream ports at 1.5A. Then apple has their own standard for their chargers using voltage dividers on data lines. Qualcomm has their own Quick charge standard which allows you to request the source to raise your voltage to 9V, 12V and beyond. There's even an adjustable version. Oh and then USB-C 3.0 changes the game even more.
So with all these possibilities you need to design a much smarter charger. Fortunately for the engineer there are solutions from TI, Maxim and others. There are ICs that attach to the USB lines and just detect what kind of charger is attached and by going through each option one at time. Maxim has a very long app note about how that is done.
If you look at a TI series part like the bq25898 series you will see it includes USB source type detection, full Lion charging control, and an integrated buck regulator. You can further complicate your system by having a small host micro processor to make higher level decisions but for the most part these parts can run autonomously.
So when you plug a USB device into your system using the above type of charger design, it runs through each type of source detection in series. Are you QuickCharge, do I see pullups, shorted D+/D-, voltage dividers on D+/D-. Once it finds one it sets an internal current limit for itself so it will not pull more power from the source. They also add in some voltage droop monitoring on the source voltage to ensure that even though it says 1.5A that it still works if the source is poorly designed and can only provide 1A.
USB-C 3.0 adds another layer of complexity. They have new pins, the CC pins and the standard defines a method of communicating over those pins to negotiate how much current/voltage the source can supply to you. They also define a way to determine if your cable is capable of supplying that current.
So anyway that's how the circuit decides how much current it can pull from the source. There is a whole nother section of the charger dedicated to charging the Lion correctly.
Oh and I should mention if your product also wants to communicate over USB there is often a small switch involved. So the charger connects itself to the lines for "charger detection" phase, then swaps the lines over to your processor when detection is complete (or not if it's in a particular charging mode).