Let $a/b \in \Q$ be an arbitrary rational number with integers $a, b$.
Then we have
\begin{align*}
f\left(\frac{a}{b}\right)&=f\left(a\cdot\frac{1}{b}\right)\\
&=f(a)f\left(\frac{1}{b}\right) && \text{ (since $f$ is a ring homomorphism)}\\
&=g(a)f\left(\frac{1}{b}\right) && \text{ (since $a$ is an integer)}\\
&=g\left(\frac{a}{b}\cdot b\right) f\left(\frac{1}{b}\right)\\
&=g\left(\frac{a}{b}\right) g(b) f\left(\frac{1}{b}\right) && \text{ (since $g$ is a ring homomorphism)}\\
&=g\left(\frac{a}{b}\right) f(b) f\left(\frac{1}{b}\right) && \text{ (since $b$ is an integer)}\\
&=g\left(\frac{a}{b}\right) f\left(b\cdot \frac{1}{b}\right) && \text{ (since $f$ is a ring homomorphism)}\\
&=g\left(\frac{a}{b}\right) f(1) \\
&=g\left(\frac{a}{b}\right) \cdot 1\\
&=g\left(\frac{a}{b}\right).
\end{align*}

Therefore, we proved
\[f\left(\frac{a}{b}\right)=g\left(\frac{a}{b}\right),\]
for any rational number $a/b\in \Q$.
Hence we have $f=g$.

Remark.

In the language of category theory, this shows that the inclusion $\Z\to \Q$ is epi in the category of rings.
Also, note that this inclusion is not epi in the category of abelian groups ($\Z$-mod).

There is Exactly One Ring Homomorphism From the Ring of Integers to Any Ring
Let $\Z$ be the ring of integers and let $R$ be a ring with unity.
Determine all the ring homomorphisms from $\Z$ to $R$.
Definition.
Recall that if $A, B$ are rings with unity then a ring homomorphism $f: A \to B$ is a map […]

The Inverse Image of an Ideal by a Ring Homomorphism is an Ideal
Let $f:R\to R'$ be a ring homomorphism. Let $I'$ be an ideal of $R'$ and let $I=f^{-1}(I)$ be the preimage of $I$ by $f$. Prove that $I$ is an ideal of the ring $R$.
Proof.
To prove $I=f^{-1}(I')$ is an ideal of $R$, we need to check the following two […]

Characteristic of an Integral Domain is 0 or a Prime Number
Let $R$ be a commutative ring with $1$. Show that if $R$ is an integral domain, then the characteristic of $R$ is either $0$ or a prime number $p$.
Definition of the characteristic of a ring.
The characteristic of a commutative ring $R$ with $1$ is defined as […]

A Maximal Ideal in the Ring of Continuous Functions and a Quotient Ring
Let $R$ be the ring of all continuous functions on the interval $[0, 2]$.
Let $I$ be the subset of $R$ defined by
\[I:=\{ f(x) \in R \mid f(1)=0\}.\]
Then prove that $I$ is an ideal of the ring $R$.
Moreover, show that $I$ is maximal and determine […]

Generators of the Augmentation Ideal in a Group Ring
Let $R$ be a commutative ring with $1$ and let $G$ be a finite group with identity element $e$. Let $RG$ be the group ring. Then the map $\epsilon: RG \to R$ defined by
\[\epsilon(\sum_{i=1}^na_i g_i)=\sum_{i=1}^na_i,\]
where $a_i\in R$ and $G=\{g_i\}_{i=1}^n$, is a ring […]

$(x^3-y^2)$ is a Prime Ideal in the Ring $R[x, y]$, $R$ is an Integral Domain.
Let $R$ be an integral domain. Then prove that the ideal $(x^3-y^2)$ is a prime ideal in the ring $R[x, y]$.
Proof.
Consider the ring $R[t]$, where $t$ is a variable. Since $R$ is an integral domain, so is $R[t]$.
Define the function $\Psi:R[x,y] \to R[t]$ sending […]

The Preimage of Prime ideals are Prime Ideals
Let $f: R\to R'$ be a ring homomorphism. Let $P$ be a prime ideal of the ring $R'$.
Prove that the preimage $f^{-1}(P)$ is a prime ideal of $R$.
Proof.
The preimage of an ideal by a ring homomorphism is an ideal.
(See the post "The inverse image of an ideal by […]