Every 2016 blocks, about two weeks, every node looks at how long those blocks took. If they came faster than ten minutes on average, the target is lowered so valid hashes become rarer. If they came slower, the target is raised.
A thermostat with no one at the controls
Nobody decides the difficulty. Each node computes it from the timestamps in the chain, with the same formula, and gets the same result. A block that does not respect the new target is rejected by everyone.
One adjustment is capped: the difficulty can change by at most a factor of four up or down. When a large share of miners switched off in 2021 after a ban in China, blocks slowed for a few weeks, then the difficulty fell and the pace returned to ten minutes.
▶ SEE IT IN THE MACHINEMINING DIFFICULTY is a gearbox with a lever: its position is the real change expected at the next retarget.