When a 240 Vrms mains voltage is rectified with a bridge rectifier and reservoir capacitor, the output voltage becomes 340 VDC, on average.
Correct, if smoothing capacitor added.
Then, I would put a buck converter that oscillates at 20 kHz (say) and step it down back to 240V. But how would you step that 340V down to 240V AND eliminate (all or as much as possible) that resultant high-frequency ripple?
You wouldn't. You'd use a 240 V AC to 240 V DC switched mode power supply and do it in one step.
Figure 1. Switched mode power supply block diagram.
What components would you add? And what should be the base voltage to the switching transistor?
You generally wouldn't use a BJT transistor. I suspect you've picked up some terminology somewhere but don't have enough understanding at present to do this project. Pick something at low voltages first.
The 240V sine wave gives 240 Vrms, but the peak-to-peak voltage is actually about 340V.
Almost right. \$240 \sqrt 2 = 340 \; V_{peak} \$. The peak to peak voltage of the AC is double that.