The best-written summary I could find of this was on Wikipedia.
Technical preliminaries (you can skip this if you don't care)
All chordophones (musical instruments based on vibrating strings) can be analyzed using the same physics model of a string under tension that is fixed on both ends. The model is slightly simplified and differs from reality in two ways: in the model, the string has zero thickness, and the string has zero stiffness. When comparing results calculated based on the model with real-world behavior of such strings, it turns out those two simplifictions do not make a significant difference in most areas. One exception to that is that the need for stretch tuning on some instruments like the piano is not predicted by the model because of the omission of stiffness.
The following discussion will be based on the model of string motion described above. When the string is at rest, no sound is produced. When the position of the string is disturbed, by plucking, striking, etc., the tension on the string acts to restore the string to its rest position. The mass of the string gives the string inertia which causes the string to overshoot its rest position and be displaced again in the opposite direction, and again the tension acts to restore the string to rest position, etc. The tension and mass interact in this way to cause the string to keep moving back and forth until something stops the motion of the string. A string that is not intentionally damped will eventually stop due to friction that converts the kinetic energy of the string into heat.
The formula (this is the math answer to your question)
In both our model and real-world strings, the back and forth motion of the disturbed string quickly settles down to a resonant frequency. That frequency is determined by the properties of the string, and based on the model can be calculated (with reasonable precision) by the following equation:
- f is the resonant frequency (the note being played)
- v is the speed of propagation of a disturbance (wave) along the length of the string
- L is the sounding length of the string (the length between the two fixed points on either end of the disturbed portion of the string)
- T is the tension of the string (along the sounding length, if the tension is not everywhere the same)
- mu is the linear density, or mass per unit length
Notice that the resonant frequency does not depend on the total mass of the string along the sounding length. In the model and formula, the length component that would be multiplied by mu to get the total mass "cancels out", so it is only the linear density that matters.
The musical consequences (read at least this)
The model and formula we have created for our vibrating strings suggest the following things about changing the resonant frequency (and therefore the note we are playing) of our strings:
- To play a higher note, we can reduce the length, reduce the linear density (in practice, the thickness), or increase the tension of a string.
- To play a lower note, we can increase the length, increase the thickness, or decrease the tension on the string.
As mentioned above, once our model hits the real world we quickly encounter a problem: the thickness of the strings (which is ignored in the model) is the easiest way to control the linear density, but has some effects on the final tone and tuning of the musical instrument. The thinner the string, the closer to the model the real string is (since the model assumes zero thickness), which makes the math of building the instrument easier. On the other hand, thicker strings tend to have a richer tone (citation needed - subjective). On the other other hand, thicker strings also have greater stiffness, which further deviates from the model and causes other problems (Aside: This is one reason why thicker strings are made from wire wrapped around wire, rather than just using a really thick piece of solid wire. Wound strings are much less stiff than solid strings of the same linear density.) There are also complicated concerns about mechanical impedance matching between strings and bridges or fixed points, etc., which I won't dive deeply into, here.
With a little experimentation, we can come up with an ideal string thickness and composition for any given instrument that uses strings. But now we start to have other problems. If we fix the thickness (linear density) of all the strings on our instrument, then the only way to change the notes those strings are playing is by changing the length and/or the tension. Tension can be a problem because the materials we use to stretch the strings have to be strong enough to withstand that tension. This is why cast iron piano frames were significant when they were introduced. On the other hand, strings that are too loose will not have as good tone or intonation. Length is a problem because again there are instrument construction concerns as well as playability. For an instrument like the guitar, it's not practical to make the strings have different unfretted sounding lengths. For an instrument like the piano, the ideal length of the lowest strings would be extremely long. Even a 9 foot grand piano is a compromise on length!
Piano construction realities
With the piano, there is an excellent opportunity for balancing the thickness, tension, and length of strings, because every note gets its own dedicated string(s)! More or less, modern pianos are built to try to have the optimum length, tension, and thickness for each note, but there are many challenges and compromises made.
Thinner strings with shorter sounding lengths may play the right note, but they also are quieter and have a shorter sustain. That is why if you look at high notes on the piano, you will see three strings used for each note. That doesn't help very much with the sustain, (notice dampers are omitted entirely for the highest notes on many pianos) but it does help even out the volume.
Thicker strings help with lowering the note a string produces, but then we have stiffness issues. We also need some tension to keep the string from being "too floppy" (technical term). So let's make the bass notes on the piano really long! Well, that causes problems with fitting the piano into a living room or on a crowded stage and making the piano very expensive. Symphony orchestras can afford to take up a lot of room and money on the best pianos, so you can see 9 foot long concert grands being played for concerti. At home, you might have a spinet with a maximum sounding length of only about three feet and a less rich bass tone to go along with that.
So, finding the ideal balance of length, tension, thickness, tone, cost, and size are all factors in determining how long each string is on a given piano.
Guitar construction realities
On a guitar, the strings all have to be approximately the same length. If they weren't, it wouldn't be a guitar and it couldn't be played the way a guitar is played. Each string also needs to be about the same tension or else the mis-matched tension on the strings can eventually deform the guitar and destroy its tone and intonation. That means that thickness is our main tool for creating the open string notes on a guitar.
That brings us to a fundamental difference between the guitar and the piano. Each note on the piano has its own string(s), whereas each string on the guitar is used to play multiple notes. It's not practical to change the thickness or tension of a string in real time to play music, so on the guitar (and all the instruments in the strings family), different notes are played on the same string by changing the sounding length of the string.
So even though the basic "open" sounding lengths of all the strings on a guitar (or violin or cello, etc.) are the same, while it's being played the sounding lengths are all changing and potentially very different. In case it's not obvious, the string lengths on the guitar are changed by fretting, i.e., pressing the string down against a metal bar (which is placed in a particular location) which acts to shorten the sounding length of the string.
So that's way too much information to simply say: It's length, tension, and thickness combined that determine the note(s) made by a string. On a piano, those are all pre-chosen and tuned before a single key is struck. On a guitar, the thickness and tension are pre-chosen, along with six basic lengths, and then the lengths are dynamically changed during performance.