How do digital pianos work?
Electronically speaking a digital piano does not necessarily work by combining different harmonics to synthesize a specific note. Most digital pianos work by playing pre-recorded samples that are generally stored as audio files on some internal memory. They keyboard loads the specific note's audio files (stored as digital data,) runs that through a DAC (digital to analog converter) which is then played out as an analog note.
Analog synthesizers are what I beleive you are asking about generally use VCO (voltage controlled oscilators) that often come in IC packages like down below:

With a setup using a VCO key presses generally translate into various voltages that the VCO circuit translates into a type of signal (sine, square, triangle).
Circuit Digest explain this in more detail at the link below.
https://circuitdigest.com/tutorial/voltage-controlled-oscillator-vco
The general system architecture of a synth would looks like this,


The keyboard generates voltages dependant on the note pressed.
VCO (voltage controlled oscillator) generate a note signal proportional to the keyboard voltage.
VCF (voltage controlled filter) would filter the VCO signal proportional to the filter controls voltage.
ADSR (attack, decay, sustain, release) this would affect the shape of the signal, how quick the note appears, how long it takes to roll off.
Noise, some synths will have the option to distort the signals with a noise generator.
If the synth is polyphonic it will combine the signal of the various oscillators before outputting.

Different types of synths use different methods and different types of oscillators, they could also include multiple VCO, filters, delay and any number of other audio effects to generate more interesting sounds.
@DKNguyen good point, some synths use a DSP (digital signal processor) to generate signals, these effectually use programs designed using mathematical formulas to calculate the desired signals. After the DSP stage these types of keyboards work in the same way as a standard 'sample' digital keyboard with a DAC and amplifier to a speaker.
This was quite a vague questions and this is a massive subject, I hope this give you a basic idea of the concept, please do a bit more googling if you want to find out more details.