Pressing the string against the neck reduces the vibrating length of the string to that between the bridge and the next fret between the fretting finger and the bridge, thus changing the pitch of the note produced.
The advantage of frets is that they make it much easier to achieve an acceptable standard of intonation. However, those who favour fretless stringed instruments will be quick to point out that this means accepting certain compromises. However, for those without the blessing/curse of perfect pitch they provide an easier way to make relatively harmonious music.
Many fretted instruments are also available in fretless versions. For example, the bass guitar and acoustic bass guitar come in both varieties. The majority are fretted, like the vast majority of guitars, but a significant number of fretless basses are also played, in part drawing on the influence of the double bass (itself, virtually always fretless).
It is also possible to find semi-fretted instruments, although these are normally one-off, custom adaptations made for players who want to combine elements of both types of sound. One arrangment is for the frets to only extend part of the way along the neck so that the higher notes can be played with the smooth expression available on a fretless instrument. Another approach is the use of frets that exend only partway across the fretboard so that some courses of strings are fretted and others fretless.
Another variant is called 'scalloping' and involves the wood between some or all of the frets being scooped out. This allows a lighter touch for faster playing and also opens up new options for altering the pitch by bending the strings with the fretting hand. It had some popularity with musicians playing heavy metal music although the ideas can also be seen in more ancient instruments such as the sitar.