The point is that when you write $g(x)=x+1$ or some other expression you're not really defining a function. A function $f$ is determined when you specify its domain $A$, its codomain $B$ and a subset $E\subseteq A\times B$ with the property that $\forall a\in A\,\exists!\,b\in B\,s.t.\,(a,b)\in E$ (and you write $b=f(a)$. )
Usually, in the first courses of analysis, it's introduced the ( rather artificial ) concept of domain of definition: given a certain expression [usually a finite composition of "elementary" real functions, (exponential, logarithm, sine, cosine, with their natural domains) and field operations over $\mathbb{R}$ (sum, difference, product, division) ], we assigns to it as domain the subset of $\mathbb{R}$ of all numbers $x$ such that the expression is "computable over $\mathbb{R}$" when calculated in $x$, i.e. $x$ must belong to the intersection of the natural domains of all the elementary functions in the expressions, and is such that you never make a division by zero when "computing" the expressions in $x$.
Now, with the domain specified in this way, you have really defined a real function (the codomain is $\mathbb{R}$).
In your case, the expressions $(x^2-1)/(x-1)$, is computable for every $x\neq 1$, for $x=1$ you get $0/0$ instead. So you defines a function $f$ with domain $D=\mathbb{R}\backslash \{1\}$, and given $x\in D$, $y\in\mathbb{R}$, you write $y=f(x)$ if $y=(x^2-1)/(x+1)$, that is $y=x+1$.
In the second case, the domain of $g$ becames $\mathbb{R}$, and you write $y=g(x)$ if $y=x+1$.
Since $f$ and $g$ have different domain, they are not the same function. But if you restrict the domain of $g$ to $\mathbb{R}\backslash \{1\}$, you get exactly $f$.