I'm realy not sure how. It only fully turned into that killer version after merging with the Sphere data. The Sphere was a several hundred thousand year old benevolant entity. It's last act was to save Discovery, and imprinted it's principles on the data it uploaded, the self aware ship protecting itsef but not attacking Enterprise, helping them take the data to the future.
How the not actually alive Control, a vicious and evil program, was able to overcome and overwrite the possibly living data of the Sphere and corrupt it's ethical coding is weird.
A conflicted emergent creature sure, but what we got just lazily went "evil AI" for the sake of the story and isn't consistent with the way around the two halves met.