Let's explain this using an example. Consider a cell in which an aqueous solution of sodium chloride is electrolyzed. The $\ce{Na^+}$ ions migrate toward the negative electrode and the $\ce{Cl^-}$ ions migrate toward the positive electrode. But indeed at the anode, there are two substances that can be oxidized at the anode: $\ce{Cl^-}$ ions and water molecules.
$$\ce{2Cl^- \rightarrow Cl_2 + 2e^-}$$ $$\ce{2H_2O \rightarrow O_2 + 4H^+ + 4e^-}$$
The standard-state potentials for these half-reactions are $E^0_{ox}= -1.36 V$ for the first couple and $E^0_{ox}= -1.23 V$ for the second couple.
At first glance, it would seem easier to oxidize water ($E^0_{ox}= -1.23 V$) than $\ce{Cl^-}$ ions ($E^0_{ox}= -1.36 V$).
It is worth noting, however, that the cell is never allowed to reach standard-state conditions. The solution is typically 25% $\ce{NaCl}$ by mass, which significantly decreases the potential required to oxidize the $\ce{Cl^-}$ ion. The pH of the cell is also kept very high, which decreases the oxidation potential for water. The deciding factor is a phenomenon known as overvoltage, which is the extra voltage that must be applied to a reaction to get it to occur at the rate at which it would occur in an ideal system.
Under ideal conditions, a potential of 1.23 volts is large enough to oxidize water to $\ce{O_2}$ gas. Under real conditions, however, it can take a much larger voltage to initiate this reaction. (The overvoltage for the oxidation of water can be as large as 1 volt.) By carefully choosing the electrode to maximize the overvoltage for the oxidation of water and then carefully controlling the potential at which the cell operates, we can ensure that only chlorine is produced in this reaction.