As far as I know, there is not an official answer to this, but the most accepted one by the fandom is that...
"The alignments of each character are based on what they think of themselves"
There is also a few more less popular theories about this system, like the alignments are decided by how the mankind views the legend of each servant.
It may even be just random bs that Nasu puts there depending on his mood
- NurAhmad4 years ago

IIRC, Some Nasu fans also mentioned that Alignment system in Fate series is based from what deeds/action/contributions the servants have done toward earth in their previous life, So even if He/She isn't considered as good anymore in our age, Because his/her actions/deeds/contributions for earth was considered good when he/she was alive, His/her alignment will be good too. I'm not sure if this true either. Nasuverse Lore is so confusing (-_-)
- NurAhmad4 years ago

"The alignments of each character are based on what they think of themselves"
But at the same time there are characters who think they doing good things for humanity
For example from FGO:
Goetia
Or from Fate Extra (IIRC):
KiaraBut their alignment are still listed as Chaotic Evil

