Data can be from a controlled, randomized experiment or from an observational study. In the former, X is randomly assigned to subjects. In the latter, it is not randomly assigned.
In randomized experiments, causal inference is straightforward. In observational (non-randomized) studies, the problem is much harder and requires stronger assumptions and also requires subject matter knowledge. Statistics and Machine Learning cannot solve causal problems without background knowledge.