There are standard finger patterns for specific scales, but you won't always be playing just the scale/key - you could be playing notes in any pattern. So you need to change the position of your hand in a way that the progression of the fingers stay (more or less) the same but is just transposed.
As an example: Consider the general finger pattern (right hand) for the A major scale:
1 2 3 1 2 3 4 5/1 2 3 1 ...
A B C# D E F# G# A B C# D ...
where 1 = thumb & 5 = little finger.
Now, consider the following line (an excerpt of Rondo Alla Turca by W.A. Mozart)
[FINGER PATTERN MARKED BY ME ACCORDING TO MY JUDGEMENT]
Note that the key signature suggests this part to be in the key of A Major. Therefore, according to the standard rule, the first note (in the bar where the numbering starts), which is an E, should be played with your 2nd finger (index finger). However, the score suggests to use your 5th finger. Here, however, you could do either - use your 2nd or 5th finger but using the given finger pattern makes it a bit convenient. Obviously it's not in accordance with the finger pattern rule of playing the complete A major scale, but we may make this change since there is provision for it in this situation: we can save ourselves the trouble of switching from the thumb to the middle finger (while moving from D to C#) since that isn't necessary - owing to the notes that follow.
Moving further, you'll see that the 5th bar suggests using your 4th finger (right hand ring finger) to play the next higher A# followed by your little finger playing the B. This is neither in accordance with the finger pattern rule for the chromatic scale nor the A major scale. However, shifting our hand across the octave to use the thumb (1st finger) instead of the 5th wouldn't be very efficient as we have to come down again in the next bar, so it's more efficient to just extend the hand a little bit and use the fingers which are closer to the notes, as we'll easily be able to position our hand to play the next pattern - which begins with the 5th finger on E. In fact, WillRoss1’s second comment contains an even better technique for this segment.
If you follow the score on a keyboard, you'll be able to make better sense of the answer.
This is NOT THE ONLY acceptable and definitely not the most efficient finger pattern. A more experienced pianist would be able to come up with something better. My aim here is just to help you get a rough idea of how the process of determining the finger pattern works.
In practice, you follow the standard patterns as and where they work and make judicious transitions and modifications to fit a certain finger pattern over a certain segment of notes in a way that “makes sense”.