That’s the job of the constant-speed propeller. The pilot sets a low or fine pitch (high rpm) for takeoff, and a higher or coarse pitch (low rpm) for cruise, using a propeller control knob or lever next the throttle.
What makes the concept work is the propeller governor. The governor takes in oil under pressure from the engine, usually about 50 to 70 pounds per square inch, and boosts it to 300 psi. That pressure is sent to the back side of the propeller, where its job is to push on a plunger that moves gears. The gears change the propeller blade angle. The plunger’s position is limited by a “speeder spring” that, in turn, is controlled by the pilot. When the plunger gets to a setting chosen by the pilot, oil is released back to the engine and the plunger maintains its position, keeping rpm constant at that setting.
The constant-speed propeller is dumb. It does whatever the governor tells it to do.