The Spanish letter X has two different pronunciations, just like its English counterpart. There is no real rule as to which X’s are pronounced which way; it’s just something to memorize for each word.
1. [ks] like "axe" and "mix" in English.
2. [gz] like "exact" and "example" in English.
Please note that this explanation is only a guideline for the Spanish that I know, which is Castilian Spanish. There are many regional variations in Spanish pronunciation.