You could write music using all twelve semitones, and some composers have. Most music instead selects a small subset, usually seven, and treats one of them as home. That selection is a scale, and the choice of which notes to include shapes everything built on top.
The major scale
The most familiar pattern in Western music is the major scale, the do re mi sequence. Its identity is a specific sequence of steps: tone, tone, semitone, tone, tone, tone, semitone.
Start on C and apply that pattern and you land exactly on the white keys: C D E F G A B, back to C. This is not a coincidence so much as a design decision made centuries ago and frozen into the keyboard.
Crucially, the pattern is portable. Start on G and follow the same sequence of steps and you get the G major scale, which needs one black key (F sharp) to keep the gaps right. Any starting note works, which is what makes transposition possible: the same tune, higher or lower, with its character intact.
Degrees and gravity
The notes of a scale are numbered by degree, and they are not equals. The first degree, the tonic, is home: melodies tend to end there and it sounds settled. The fifth degree, the dominant, is the strongest other pole. The seventh, the leading note, sits one semitone below the tonic and leans on it hard, which is why a tune stopping there feels unfinished.
This sense of gravity, of some notes pulling toward others, is called tonality, and it is what lets a listener feel a phrase resolve without knowing any theory at all.
Minor
Change the pattern and you change the mood. The natural minor scale runs tone, semitone, tone, tone, semitone, tone, tone. The important difference is the third degree, which is now three semitones above the tonic instead of four.
That single semitone accounts for most of the difference between music that sounds bright and music that sounds sombre. Minor comes in variants, notably the harmonic minor, which raises the seventh degree back up so the leading note keeps its pull toward home, at the cost of an unusually wide gap that gives the scale its distinctive, slightly exotic colour.
Other selections
The seven-note scales are not the only options. The five-note pentatonic scale, made by dropping the two notes that create the sharpest clashes, appears independently in folk music across Asia, Africa, Europe, and the Americas, and it is nearly impossible to play a wrong note in. The blues scale adds a flattened note that sits deliberately between the cracks. What all of them share is a home note and a pattern, and from those patterns come chords.