5. Find all functions $f: \mathbf{R} \rightarrow \mathbf{R}$, such that for all $x, y \in \mathbf{R}$, we have
$$
f(1+x y)-f(x+y)=f(x) f(y),
$$

and $f(-1) \neq 0$.
5. The unique solution that satisfies the condition is the function
$$
f(x)=x-1(x \in \mathbf{R}) \text {. }
$$

Let $g(x)=f(x)+1$.
First, prove that for all real numbers $x$, $g(x)=x$.
The original condition is transformed into: for all $x, y \in \mathbf{R}$,
$$
\begin{array}{l}
g(1+x y)-g(x+y) \\
=(g(x)-1)(g(y)-1),
\end{array}
$$

and $g(-1) \neq 1$.
Let $C=g(-1)-1 \neq 0$.
In equation (1), let $y=-1$, we get
$$
g(1-x)-g(x-1)=C(g(x)-1) \text {. }
$$

In equation (2), let $x=1$, we get $C(g(1)-1)=0$.
Since $C \neq 0$, it follows that $g(1)=1$.
In equation (2), let $x=0$, we get
$$
-C=C(g(0)-1) \Rightarrow g(0)=0 \text {; }
$$

Let $x=2$, we get
$$
C=C(g(2)-1) \Rightarrow g(2)=2 \text {. }
$$

Next, prove that for all $x \in \mathbf{R}$,
$$
g(x)+g(2-x)=2 \text {, }
$$
$$
g(x+2)-g(x)=2 \text {. }
$$

In equation (2), replace $x$ with $1-x$ to get
$$
g(x)-g(-x)=C(g(1-x)-1) \text {. }
$$

In the above equation, replace $x$ with $-x$ to get
$$
g(-x)-g(x)=C(g(1+x)-1) \text {. }
$$

Adding the two equations, we get
$$
C(g(1-x)+g(1+x)-2)=0 \text {. }
$$

Since $C \neq 0$, it follows that
$$
g(1-x)+g(1+x)=2 \text {. }
$$

Replacing $x$ with $1-x$, we obtain equation (3).
Let $u, v$ satisfy $u+v=1$.
In equation (1), let
$$
(x, y)=(u, v) \text { and }(2-u, 2-v) \text {, }
$$

we get $g(1+u v)-g(1)=(g(u)-1)(g(v)-1)$, $g(3+u v)-g(3)=(g(2-u)-1)(g(2-v)-1)$.
By equation (3),
$$
\begin{array}{l}
g(2-u)-1=2-g(u)-1=1-g(u), \\
g(2-v)-1=2-g(v)-1=1-g(v) . \\
\text { Hence }(g(u)-1)(g(v)-1) \\
=(g(2-u)-1)(g(2-v)-1) .
\end{array}
$$

Thus, $g(1+u v)-g(1)=g(3+u v)-g(3)$, which means for $u+v=1$, we have
$$
g(u v+3)-g(u v+1)=g(3)-g(1) .
$$

For each real number $x \leqslant \frac{5}{4}$, it can be expressed as $x=$ $u v+1$, and $u+v=1$. This is because when $x \leqslant \frac{5}{4}$, the discriminant of the quadratic equation $t^{2}-t+(x-1)=0$ is greater than or equal to 0, so there are real roots $u, v$. This shows that for all real numbers $x \leqslant \frac{5}{4}$, we have

In summary, for all $x \in \mathbf{R}$, $f(x)=x-1$.
Direct verification shows that this function satisfies the original equation.