ch [x] passed early into Latin as
c with the sound [k]. Then a sound change – still in Latin – made c- different before -e and -i, which Spanish inherited (after another sound change).
K works mostly for new or specialized scientific terms. Some of them may become written with q with time, as they are adopted into mainstream language (k tends to sound less Spanish, though there are exceptions). The choice of c (and its sound) usually obeys the history of the word, for example:
- A less educated origin of the word as the question linked by @Rodrigo suggests (as in cirugía vs quirúrgico).
- The fact that there was a so-to-say more Spanish way of naming the concept: e.g. the adoption of a new scientific meaning for a word already existent in Spanish through Latin. Cetona has a complicated history but ultimately is a modification of French acétone which has the Spanish cognate acetona.
- The fact that the scientific term was originally coined in Latin (at the time it was the language of science), like in leucemia.
The case of química is different and involves Arabic (probably some other words followed the same process: you'd be surprised how an 8-century-long invasion can influence a language). Even being Greek in origin it lost the
ch [x] sound when adopted by Arabic, but preserved the closer
k sound. Since it is anything but a neologism, the logical orthographic rule to follow is the regular one (i.e., representing [ki] sound as qui).
Hope it helps.