Effective Cartier divisors

A Cartier divisor on a scheme $X$ is effective if it can be represented by $\{(U_i,f_i)\}$ where $U_i$ covers $X$ and $f_i \in X$ Let $\mathcal{I}$ be a sheaf of ideals which is locally generated by $f_i$. I know that $\mathcal{I}$ defines the closed subscheme, say $Y$

In Hartshorne book, $Y$ has codimension $1$. But I don't know why $Y$ has codimension $1$.

The statement is not true as stated: for example if $f_i=0$ for all $i$, then $Y=X$ is of codimension zero!
Similarly you get problems if all $f_i=1$.
You should suppose something like $X$ locally noetherian and the $f_i$'s not locally zero divisors nor invertible.
You can then assume that $X=Spec(A)$ is affine and conclude by Krull's Hauptidealsatz (=theorem on principal ideals):
If $A$ is a notherian ring and if $f\in A$ is neither invertible nor a zero divisor, then every prime ideal minimal over $f$ has height $1$.
The elements $f_i$ in $\mathcal O(U_i)$ are required to be regular, i.e. non-zero divisors, and then by the Hauptidealsatz they cut out a scheme $Y \cap U_i$ of codimesion $1$ in $U_i$.