The entire sky and all airports are divided into ATC areas of responsibility. Whenever a flight moves from one area of responsibility to another, the pilot needs to change frequency to talk to the next controller. A flight can only be under the control of one single ATC unit at any one time, which usually equates to being on one single radio frequency. When under ATC control, pilots do not change frequencies unless instructed to do so.
For the en-route phase, airspace is divided into sectors, defined as three dimensional areas. Each sector will typically have its own dedicated frequency, although sectors can be combined in which case several sectors may operate on the same frequency.
On the ground, depending on the size of the airport, there can be multiple different frequencies. It is common to have one tower frequency per runway, and then one frequency for the taxiways - or possible several, if the airport is large and complex.
ATC will instruct the pilot to change to the next frequency at the latest when the flight enters the new area of responsibility, and often a minute or two before that. At that point, the flight will already have been coordinated between the controllers, so the controller of the downstream area of responsibility knows it's coming. Specific rules for transfer of communication is agreed between adjacent ATC units in letters of agreement.
An example from Copenhagen:
AIP Denmark EK AD 2 - EKCH - AREA OF RESPONSIBILITY
Each color depicts an area of responsibility, and (this is a bit simplified but) essentially one specific frequency that pilots should be on when they are within that area. You can see that each runway (roughly) has one frequency, and the apron area where aircraft park has another. When taxiing from one area to another, the controller will instruct the pilot to change to the next frequency.
In the air, it can look something like this:
AIP Denmark EK ENR 6.2 - 3
The dashed lines indicate borders between areas of responsibility, and the bold text indicates the frequency to use in that area. Again, the controller will instruct the pilot to change to the next frequency when crossing from one area to another.