There is a small mistake in your interpretation, let me take a detailed stab at it, for someone trying to get the essence of it, else the original answer provided by Silverfish suffices.
When you say:
$:> mod <- lm(dist ~ speed, data=cars)
That means, you are using 1 predictor, which is speed but the lm() function will add a bias term by default. The linear regression model used would be a simple linear regression (i.e. just one predictor and two parameters) and it's equation would be:
$Y = \beta_0 + \beta_1speed + \varepsilon$
Where, $\beta_0$ was added by default.
Now, let's try to compute $H$ matrix and find it's trace:
$HY = \widehat Y$
$=> HY=X \widehat \beta$
Using OLS, $\widehat \beta$ could be shown as: $(X^TX)^{-1}X^TY$
$=> HY=X . (X^TX)^{-1}X^TY$
$=> H = X(X^TX)^{-1}X^T$
Now this is your final H matrix.
Here, let $A = X(X^TX)^{-1}$ and $B = X^T$
Applying commutative property of the trace operator: $tr(AB) = tr(BA)$ which could be easily derived (just try multiplying two matrices and summing their diagonals):
$trace(H) = tr(X (X^TX)^{-1} X^T) = tr(AB) = tr(BA) = tr(X^T . X (X^TX)^{-1} ) = tr(I_{X^TX}) = tr(I_{(p+1) \times n . n \times (p+1)} = tr(I_{p+1 \times p+1}) = tr(I_{p+1}) = tr(I_{p+1}) = \sum^{p+1}_{i=1} (1) = p+1$
Therefore, $trace(H) = p+1$
Now, for your fitted SLR model mod where number of predictors were 1 (i.e. p=1)
$trace(H) = p+1 = 1+1 = 2$
That is what you get, when you perform using the model function:
$:> H_diagonals = lm.influence(mod)$hat
$:> trace_H = sum(H_diagonals)
$:> cat("\nTrace(H) using model:", trace_H)
[1] Trace(H) using model: 2
You could compute this by hand as well:
$:> # Design Matrix, shape: n x (p+1)
$:> X = cbind(1, cars$speed)
$:> XTX_inverse = solve(t(X) %% X)
$:> H_byhand = X %% XTX_inverse %*% t(X)
$:> trace_byhand = tr(H_byhand)
$:> cat("\nTrace(H) by hand: ", trace_byhand)
[2] Trace(H) by hand: 2
Now, here only while doing it by hand, if you don't add ones in the design matrix, and leave your X to have a shape of n x p (i.e. n x 1) it will result in $trace(H) = p = 1$ which you are getting. So ideally, you are taking a wrong format of design matrix (X).
It should always be:
$\begin{bmatrix} y_{1} \\ y_{2} \\ \vdots \\ y_{n} \end{bmatrix} = \begin{bmatrix} 1 & x_{1,1} & x_{1,2} & ... & x_{1,p} \\ 1 & x_{2,1} & x_{2,2} & ... & x_{2,p} \\ \vdots & \ddots & \ddots & \vdots \\ 1& x_{n,1} & x_{n,2} & ... & x_{n,p} \end{bmatrix} * \begin{bmatrix} \beta_{0} \\ \beta_{2} \\ \vdots \\ \beta_{p} \end{bmatrix} + \begin{bmatrix} \epsilon_{1} \\ \epsilon_{2} \\ \vdots \\ \epsilon_{n} \end{bmatrix}$
In your case (SLR):
$\begin{bmatrix} y_{1} \\ y_{2} \\ \vdots \\ y_{n} \end{bmatrix} = \begin{bmatrix} 1 & x_{1,1} \\ 1 & x_{2,1} \\ \vdots & \vdots \\ 1& x_{n,1} \end{bmatrix} * \begin{bmatrix} \beta_{0} \\ \beta_{1} \end{bmatrix} + \begin{bmatrix} \epsilon_{1} \\ \epsilon_{2} \\ \vdots \\ \epsilon_{n} \end{bmatrix}$
Where, X has a dimension of $n \times (p+1)$ which is $n \times 2$
Hope it clarifies your doubt. :)