How does the charger and device together accomplish these two
different voltage requirements? What electronic component is required
for this?
This function is accomplished via "Power Delivery" protocol.
The charger and devices do have special ICs that usually accomplish two basic function.
First, the ICs on each side check for CC voltage levels and determine that (a) a connection has been made and (b) who is provider and who is consumer. This information usually gets communicated to system's embedded controller (EC) in the device/system, and to the charger IC itself.
There could be four combinations at the connect moment, provider-provider, consumer-consumer, provider-consumer, and consumer-provider.
In case of "charger"(as provider), the phone will connect as provider-consumer.
In case of the phone, as soon as the connect event is established, the charger will provide "+5Vsafe" voltage on VBUS. If the phone has no extra capabilities as a consumer, it will charge itself in accord with basic CC pull-up scheme, 900ma, 1.5A, or 3A.
In case of a laptop, the process is a bit more complicated. By default, laptop's port is USB host and therefore VBUS provider. However, since it can be charged via that very same port, it is called "Dual-Role-Power" port, DRP.
As a DRP, the port will alternate CC advertising, from provider to consumer and back, periodically, few times per second.
As soon as the laptop senses that a provider is plugged in, the port will stay in consumer mode (pull CC pin down), and as soon as the charger sees the consumer, it will assert +5Vsafe VBUS.
Here comes the second function of these special ICs: both sides of the link will exchange Power-Delivery messages, which go along CC wires in a serial form, and carry information of the source capabilities, and about sink requirements. I think the details of this exchange will overload this answer, so the bottom line is: the link "partners" negotiate "power contract" to the best common abilities, and the charger will switch into higher power mode.
If your phone has higher capabilities as a consumer, the above process will take place as well. The phone also can have USB host capability, so the process won't differ much from the one described above.