From the Wikipedia page "Phonological history of Spanish coronal fricatives" (https://en.m.wikipedia.org/wiki/Phonological_history_of_Spanish_coronal_fricatives):
Distinction (Spanish: distinción) refers to the differentiated pronunciation of the two Spanish phonemes written ⟨s⟩ and ⟨z⟩ or ⟨c⟩ (only before ⟨e⟩ or ⟨i⟩, the so-called "soft" ⟨c⟩): ⟨s⟩ represents a voiceless alveolar sibilant /s/ (either laminal as in English, or apical); ⟨z⟩ and soft ⟨c⟩ represent a voiceless dental fricative /θ/ (the ⟨th⟩ in think). This pronunciation is the standard on which Spanish orthography was based, and it is universal in Central and Northern parts of Spain, except for some bilingual speakers of Catalan and Basque, according to Hualde (2005). Thus, in Spanish the choice between the spellings ⟨sa⟩, ⟨se⟩, ⟨si⟩, ⟨so⟩, ⟨su⟩ and ⟨za⟩, ⟨ce⟩, ⟨ci⟩, ⟨zo⟩, ⟨zu⟩ is determined by the pronunciation in most of Spain, unlike English, where it is often done according to etymology or orthographic conventions (although in English, soft c is always /s/ and never /z/ like s is, as with 'rise' vs. 'rice').
And from "History of the Spanish language" (https://en.m.wikipedia.org/wiki/History_of_the_Spanish_language):
During the 16th century, the three voiced sibilant phonemes—dental /d͡z/, apico-alveolar /z/, and palato-alveolar /ʒ/ (as in Old Spanish fazer, casa, and ojo, respectively) lost their voicing and merged with their voiceless counterparts: /t͡s/, /s/, and /ʃ/ (as in caçar, passar, and baxar respectively). The character ⟨ç⟩, called ⟨c⟩ cedilla, originated in Old Spanish but has been replaced by ⟨z⟩ in the modern language... Additionally, the affricate /t͡s/ lost its stop component, to become a laminodental fricative, [s̪]. As a result, the sound system then contained two sibilant fricative phonemes whose contrast depended entirely on a subtle distinction between their places of articulation: apicoalveolar, in the case of the /s/, and laminodental, in the case of the new fricative sibilant /s̪/, which was derived from the affricate /t͡s/. The distinction between the sounds grew in the dialects of northern and central Spain by paradigmatic dissimilation, but dialects in Andalusia and the Americas merged both sounds. The dissimilation in the northern and central dialects occurred with the laminodental fricative moving forward to an interdental place of articulation, losing its sibilance to become [θ]. The sound is represented in modern spelling by ⟨c⟩ before ⟨e⟩ or ⟨i⟩ and by ⟨z⟩ elsewhere. In the south of Spain, the deaffrication of /t͡s/ resulted in a direct merger with /s/, as both were homorganic, and the new phoneme became either laminodental [s̪] ("seseo", in the Americas and parts of Andalusia) or [θ] ("ceceo", in a few parts of Andalusia). In general, coastal regions of Andalusia preferred [θ], and more inland regions preferred [s̪]