You are conflating two types of "error" term. Wikipedia actually has an article devoted to this distinction between errors and residuals.
In an OLS regression, the residuals (your estimates of the error or disturbance term) $\hat \varepsilon$ are indeed guaranteed to be uncorrelated with the predictor variables, assuming the regression contains an intercept term.
But the "true" errors $\varepsilon$ may well be correlated with them, and this is what counts as endogeneity.
To keep things simple, consider the regression model (you might see this described as the underlying "data generating process" or "DGP", the theoretical model that we assume to generate the value of $y$):
$$y_i = \beta_1 + \beta_2 x_i + \varepsilon_i$$
There is no reason, in principle, why $x$ can't be correlated with $\varepsilon$ in our model, however much we would prefer it not to breach the standard OLS assumptions in this way. For example, it might be that $y$ depends on another variable that has been omitted from our model, and this has been incorporated into the disturbance term (the $\varepsilon$ is where we lump in all the things other than $x$ that affect $y$). If this omitted variable is also correlated with $x$, then $\varepsilon$ will in turn be correlated with $x$ and we have endogeneity (in particular, omitted-variable bias).
When you estimate your regression model on the available data, we get
$$y_i = \hat \beta_1 + \hat \beta_2 x_i + \hat \varepsilon_i$$
Because of the way OLS works*, the residuals $\hat \varepsilon$ will be uncorrelated with $x$. But that doesn't mean we have avoided endogeneity — it just means that we can't detect it by analysing the correlation between $\hat \varepsilon$ and $x$, which will be (up to numerical error) zero. And because the OLS assumptions have been breached, we are no longer guaranteed the nice properties, such as unbiasedness, we enjoy so much about OLS. Our estimate $\hat \beta_2$ will be biased.
$(*)$ The fact that $\hat \varepsilon$ is uncorrelated with $x$ follows immediately from the "normal equations" we use to choose our best estimates for the coefficients.
If you are not used to the matrix setting, and I stick to the bivariate model used in my example above, then the sum of squared residuals is $S(b_1, b_2) = \sum_{i=1}^n \varepsilon_i^2 = \sum_{i=1}^n (y_i-b_1 - b_2 x_i)^2$ and to find the optimal $b_1 = \hat \beta_1$ and $b_2 = \hat \beta_2$ that minimise this we find the normal equations, firstly the first-order condition for the estimated intercept:
$$\frac{\partial S}{\partial b_1} = \sum_{i=1}^n -2(y_i-b_1 - b_2 x_i) = -2 \sum_{i=1}^n \hat \varepsilon_i = 0$$
which shows that the sum (and hence mean) of the residuals is zero, so the formula for the covariance between $\hat \varepsilon$ and any variable $x$ then reduces to $\frac{1}{n-1} \sum_{i=1}^n x_i \hat \varepsilon_i$. We see this is zero by considering the first-order condition for the estimated slope, which is that
$$\frac{\partial S}{\partial b_2} = \sum_{i=1}^n -2 x_i (y_i-b_1 - b_2 x_i) = -2 \sum_{i=1}^n x_i \hat \varepsilon_i = 0$$
If you are used to working with matrices, we can generalise this to multiple regression by defining $S(b) = \varepsilon' \varepsilon = (y-Xb)'(y-Xb)$; the first-order condition to minimise $S(b)$ at optimal $b = \hat \beta$ is:
$$\frac{dS}{db}(\hat\beta) = \frac{d}{db}\bigg(y'y - b'X'y - y'Xb + b'X'Xb\bigg)\bigg|_{b=\hat\beta} = -2X'y + 2X'X\hat\beta = -2X'(y - X\hat\beta) = -2X'\hat \varepsilon = 0$$
This implies each row of $X'$, and hence each column of $X$, is orthogonal to $\hat \varepsilon$. Then if the design matrix $X$ has a column of ones (which happens if your model has an intercept term), we must have $\sum_{i=1}^n \hat \varepsilon_i = 0$ so the residuals have zero sum and zero mean. The covariance between $\hat \varepsilon$ and any variable $x$ is again $\frac{1}{n-1} \sum_{i=1}^n x_i \hat \varepsilon_i$ and for any variable $x$ included in our model we know this sum is zero, because $\hat \varepsilon$ is orthogonal to every column of the design matrix. Hence there is zero covariance, and zero correlation, between $\hat \varepsilon$ and any predictor variable $x$.
If you prefer a more geometric view of things, our desire that $\hat y$ lies as close as possible to $y$ in a Pythagorean kind of way, and the fact that $\hat y$ is constrained to the column space of the design matrix $X$, dictate that $\hat y$ should be the orthogonal projection of the observed $y$ onto that column space. Hence the vector of residuals $\hat \varepsilon = y - \hat y$ is orthogonal to every column of $X$, including the vector of ones $\mathbf{1_n}$ if an intercept term is included in the model. As before, this implies the sum of residuals is zero, whence the residual vector's orthogonality with the other columns of $X$ ensures it is uncorrelated with each of those predictors.

But nothing we have done here says anything about the true errors $\varepsilon$. Assuming there is an intercept term in our model, the residuals $\hat \varepsilon$ are only uncorrelated with $x$ as a mathematical consequence of the manner in which we chose to estimate regression coefficients $\hat \beta$. The way we selected our $\hat \beta$ affects our predicted values $\hat y$ and hence our residuals $\hat \varepsilon = y - \hat y$. If we choose $\hat \beta$ by OLS, we must solve the normal equations and these enforce that our estimated residuals $\hat \varepsilon$ are uncorrelated with $x$. Our choice of $\hat \beta$ affects $\hat y$ but not $\mathbb{E}(y)$ and hence imposes no conditions on the true errors $\varepsilon = y - \mathbb{E}(y)$. It would be a mistake to think that $\hat \varepsilon$ has somehow "inherited" its uncorrelatedness with $x$ from the OLS assumption that $\varepsilon$ should be uncorrelated with $x$. The uncorrelatedness arises from the normal equations.