There are two parts to this answer. The first is that the entropy change is defined only for reversible processes. The second is that the entropy is a state function. Many, but not all, the functions in thermodynamics (including the internal energy) are state functions.
A state function is one that is independent of path. That is, if one goes from state $A$ to state $B$, the change in internal energy is independent of how the change takes place. The same is true for the entropy.
So the entropy change in going from state $A$ to state $B$ is independent of how the change takes place. But we can calculate the change ONLY for reversible changes.
So we mentally find a reversible path from state $A$ to state $B$ and calculate the entropy change. Since entropy is a state function, that must also be the change for a non-reversible path.