Rings for which no polynomial induces the zero function
$R$ has a nonzero polynomial that induces the zero function if and only if there are ideals $I$, $J$ such that $I$ is nontrivial, $IJ=0$, and $R/J$ is a ring satisfying the following condition:
There exists $n$ such that, for any $n$ elements $x_1, \dots x_n \in R/J$, the discriminant $\prod_{i<j} (x_i-x_j)=0$.
So $R$ is glued together out of an arbitrary ring and a ring that satisfies a specific polynomial identity.
Proof of only if: Let $I$ be the ideal generated by the coefficients of the nonzero polynomial and let $J$ be the ideal of zero divisors of $I$. We will show that for all $n$ elements $x_1,\dots x_n \in R$, $I\prod_{i<j} (x_i-x_j)=0$, so that $R/J$ satisfies this identity. To see this, take any $n$ elements $x_1,\dots,x_n\in R$ and take the Vandermonde matrix. Multiplying this by the vector of coefficients of the nonzero polynomial gives $0$. So multiplying on the other side by the adjugate matrix we still get $0$, but a matrix times its adjugate is just the identity times the determinant, so the determinant time each coefficient of the polynomial is $0$. The determinant of the Vandermonde matrix is just the discriminant $\prod_{i<j} (x_i-x_j)$.
Proof of if: Let $a$ be a nonzero element of $I$, let $n$ be the smallest $n$ such that $a\prod_{i<j} (x_i-x_j)=0$ for all $x_1,\dots,x_n \in R$, so that we have some $y_1,\dots y_{n-1}\in R$ that satisfy $a\prod_{i<j} (y_i-y_j)\neq0$ . Then all $x$ satisfy:
$$a\prod_{i<j} (y_i-y_j) \prod_{i=1}^{n-1} (x-y_i)=0$$
and the leading coefficient of that polynomial is nonzero.
Since $R/I$ is arbitrary, I think we should study rings satisfying $\prod_{i<j} (x_i-x_j)=0$. Such rings have all residue fields bounded in size by $n$, so all prime ideals are maximal and their spectra are totally disconnected topological spaces. Since the geometry of this kind of space is not very easy to classify, we might want to restrict our attention to the local rings:
If $R$ is a local ring with maximal ideal $m$, there is some $n$ such that, for all $x_1,\dots x_n \in R$, $\prod_{i<j} (x_i-x_j)=0$ if and only if $R/m$ finite and there is some $N$ such that every element $x\in m$ satisfies $x^N=0$.
Proof of only if: If $R/m$ is infinite, take $x_1,\dots x_n$ to be lifts of distinct elements in $R/m$. Set $N=\frac{n^3-n}{6}$. For all $x \in m$ we can take $x_i= x^i$ for $1\leq i \leq n$. Then
$$0=\prod_{i<j} (x_i-x_j) = \prod_{i<j} x^i (1-x^{j-i})$$
which is $x^{\frac{n^3-n}{6}}$, times a unit, so $x^N=0$.
Proof of if: If $a_1,\dots,a_m$ are lifts of all the elements of the residue field, $\prod_{i=1}^m (x-a_i)^N$ is monic and vanishes for all $x$, so the bottom row of the Vandermonde matrix of any $x_1,\dots, x_{mN}$ is a weighted sum of the previous rows, and so the determinant of the Vandermonde vanishes.
I don't think one can improve the classification much beyond this. Consider the examples $\mathbb Z[a,b]/(a^2,ab,2a)$, which is just slightly off from a nice infinite integral domain, but every element satisfies the identity $a x(x-1)=0$, and $\mathbb F_2[a_1,a_2,\dots]/(a_1^2,a_2^2,\dots)$, which is an extension of Manny Reyes's example where every element satisfies the identity $x^2(x-1)^2=0$. But I would be excited to see any further insights.
I can prove a cleaner criterion in the Noetherian case. This does not carry over to the general case, as shown by Will Sawin's example in the comments below.
Lemma. Let $(R,\mathfrak p)$ be an Artinian local ring, and let $f \in R[x]$ of degree $n$. If there exists a set $S \subseteq R$ with $|\operatorname{im}(S \to R/\mathfrak p)| > n$ such that $f(r) = 0$ for all $r \in S$, then $f = 0$.
Proof. We proceed by induction on the length of $R$. The case $\ell(R) = 1$ is the case where $R$ is a field, which is trivial (this is where the hypothesis $|S| > n$ is used). In general, let $(R,\mathfrak p)$ be local Artinian. Then we have a filtration $$0 = I_0 \subsetneq \ldots \subsetneq I_s = R$$ of $R$-submodules (i.e. ideals) such that each of the subquotients $I_i/I_{i-1}$ has length $1$. Let $R' = R/I_1$, and note that $\ell(R') = \ell(R) - 1$. Therefore, by the induction hypothesis, we have $f \in I_1 R[x]$. Since $\ell(I_1) = 1$, we have $I_1 \cong R/\mathfrak p$. Thus, $I_1$ is principal, say $I_1 = (r_1)$, and $\operatorname{Ann}(r_1) = \mathfrak p$.
Hence $f = r_1 g$ for some $g \in R[x]$, which we may assume has degree $n$. Then $r_1g(r) = 0$ for all $r \in S$, so $g(r) \in \operatorname{Ann}(r_1) = \mathfrak p$ for all $r \in S$. Applying the induction hypothesis this time to $R/\mathfrak p$, we see that $g \in \mathfrak p R[x]$. Hence, $r_1 g = 0$, since $\mathfrak p \cdot r_1 = 0$. $\square$
Theorem. If $R$ is Noetherian, then there exists $f \in R[x]\setminus\{0\}$ of degree $\leq n$ with $f(r) = 0$ for all $r \in R$ if and only if $R$ has an associated prime $\mathfrak p$ such that $|R/\mathfrak p| \leq n$.
Proof. If $\mathfrak p = \operatorname{Ann}(r_0)$ is such that $|R/\mathfrak p| \leq n$, then let $\{r_1,\ldots,r_m\}$ be a set of representatives for $R/\mathfrak p$, and consider $f = r_0 \prod_{i=1}^m (x-r_i)$. Then for each $r \in R$, we have $\prod (r-r_i) \in \mathfrak p$, hence $f(r) = 0$ since $\mathfrak p \cdot r_0 = 0$. Moreover, the degree of $f$ is $m \leq n$.
Conversely, assume $|R/\mathfrak p| > n$ for every associated prime $\mathfrak p \subseteq R$, and let $f \in R[x]$ of degree $\leq n$ be such that $f(r) = 0$ for all $r \in R$. The natural map $R \to \prod_{\mathfrak p \in \operatorname{Ass}(R)} R_{\mathfrak p}$ is injective [Tag 0331]. Let $S_\mathfrak p \subseteq R_\mathfrak p$ be the image of $R \to R_\mathfrak p$ and let $f_\mathfrak p$ be the image of $f$ in $R_\mathfrak p$, for each $\mathfrak p \in \operatorname{Ass}(R)$.
Since $|R/\mathfrak p| > n$, the image of $S_\mathfrak p$ in $R_\mathfrak p/\mathfrak p = \operatorname{Frac}(R/\mathfrak p)$ has more than $n$ elements. Applying the lemma above to $R_\mathfrak p/\mathfrak p^m$ with the set $S_\mathfrak p/\mathfrak p^m$, we get that $f_\mathfrak p \in \mathfrak p^mR_\mathfrak p[x]$. By Krull's intersection theorem [Tag 00IP], this implies $f_\mathfrak p = 0$. Since this holds for all associated primes $\mathfrak p$, we conclude that $f = 0$. $\square$
Corollary. If $R$ is Noetherian, then there exists $f \in R[x]\setminus\{0\}$ with $f(r) = 0$ for all $r \in R$ if and only if $R$ has an associated prime $\mathfrak p$ such that $R/\mathfrak p$ is finite. $\square$
Remark. The first part of the proof of the does not use the Noetherian hypothesis. Moreover, the argument actually works when there exists any ideal $I$ (not necessarily prime) occurring as the annihilator of some element such that $R/I$ is finite.
Although in general rings not every annihilator is contained in an associated prime, this is true for annihilators $I$ such that $R/I$ is finite. Indeed, there are only finitely many primes in $R/I$, and a maximal one occurring as an annihilator is associated (see Eisenbud's Commutative Algebra, Prop. 3.4). Thus, allowing such $I$ does not give a more general class of examples.
As I am not allowed to write comments, I write this as an answer, although it is only a partial answer.
- If $(R,\mathfrak{m})$ is local with $R/\mathfrak{m}$ finite and $\mathfrak{m}^n=0$ for some $n$, then there is a bad polynomial for $R$. It is obtained as in Manny Reyes comment. In this case one can choose a bad monic polynomial $P$ for $R/\mathfrak{m}$ (as in the question) and hence $P^n$ is not $0$.
- The assumption that $\mathfrak{m}^n=0$ for some $n$ is necessary here. As the following example shows: $$R=\mathbb{F}_p[t,t^{1/p},t^{1/p^2},t^{1/p^3},\dots]/(t^p)$$ is a local ring with maximal ideal $\mathfrak{m}=(t,t^{1/p},t^{1/p^2},t^{1/p^3},\dots)$ and every element in $\mathfrak{m}$ is nilpotent. But for every polynomial $P(x)\in R[x]$ there is some $t^q$ such that $P(t^q)\ne 0$.