Let me make it clear that "$x^2+1$" is an expression, not a function. You can define $f : \mathbb{R}→\mathbb{R}$ by $f(x) = x^2+1$ for every $x∈\mathbb{R}$, but you can also define $f : \mathbb{R}→\mathbb{R}$ by $f(y) = y^2+1$ for every $y∈\mathbb{R}$, and the $f$ is identical whichever way you choose. As Azif00 said, the derivative of $f$ in your question is not $2x$. "$2x$" is meaningless because $x$ is undefined (it is not associated with your function $f’$ even if you used the dummy variable $x$ to define $f$). The derivative of $f$ is the function $f' : \mathbb{R}→\mathbb{R}$ defined by $f'(x) = 2x$ for every $x∈\mathbb{R}$.
Your teacher is completely correct in saying the derivative of $x^2+1$ with respect to $x$ is $2x$. Symbolically,$\def\lfrac#1#2{{\large\frac{#1}{#2}}}$ $\lfrac{d(x^2+1)}{dx} = 2x$. This can be interpreted in two ways. The first way, as explained in this post, is that this statement is made in the context where $x$ is a variable in the older sense of the word, namely a varying quantity, which is the usual context in applications of calculus to physical sciences.
The second way is a purely algebraic interpretation via "formal derivatives", where an algebraic expression can be formally differentiated with respect to any variable in it (here "variable" is in the modern sense of "symbol"). This notion of formal differentiation is useful in many branches of mathematics including field theory and combinatorics. It is also how we can differentiate any given expression with respect to a variable purely algorithmically, by repeatedly applying the sum rule, product rule and chain rule to reduce the differentiation to the base cases. For the example in your question: $\lfrac{d(x^2+1)}{dx} = \lfrac{d(x^2)}{dx} + \lfrac{d(1)}{dx}$ $= 2x + 0 = 2x$. Note that using formal differentiation will yield the correct algebraic form of the answer, but if you want to apply it to a concrete situation then you will need to check the conditions under which the rules apply.
For example, given a (moving) particle with coordinates $(x,y)$ such that $x^2+y^2=r^2$ where $r$ is a constant, we can formally differentiate with respect to $x$ to get:
$\lfrac{d(x^2+y^2)}{dx} = \lfrac{d(r^2)}{dx}$.
$\lfrac{d(x^2)}{dx} + \lfrac{d(y^2)}{dx} = 0$.
$2x + \lfrac{d(y^2)}{dy}·\lfrac{dy}{dx} = 0$. [by chain rule]
$2x + 2y·\lfrac{dy}{dx} = 0$.
But the final equation does not hold when $y = 0$, and this is because the step using the chain rule is valid only when $\lfrac{dy}{dx}$ is defined, and it is in fact not defined when $y = 0$.