Yes, your understanding of a one-to-one function is correct.
A function is onto if and only if for every $y$ in the codomain, there is an $x$ in the domain such that $f(x) = y$.
So in the example you give, $f:\mathbb R \to \mathbb R,\quad f(x) = 5x+2$, the domain and codomain are the same set: $\mathbb R.\;$ Since, for every real number $y\in \mathbb R,\,$ there is an $\,x\in \mathbb R\,$ such that $f(x) = y$, the function is onto. The example you include shows an explicit way to determine which $x$ maps to a particular $y$, by solving for $x$ in terms of $y.$ That way, we can pick any $y$, solve for $f'(y) = x$, and know the value of $x$ which the original function maps to that $y$.
Side note:
Note that $f'(y) = f^{-1}(x)$ when we swap variables. We are guaranteed that every function $f$ that is onto and one-to-one has an inverse $f^{-1}$, a function such that $f(f^{-1}(x)) = f^{-1}(f(x)) = x$.
1) Let's show this function is $1-1$. To do this, we suppose $f(n_1) = f(n_2)$ and show that this forces $n_1 = n_2$.
So, if it's true that $f(n_1) = f(n_2)$, then this means that
$$4n_1 + 1 = 4n_2 + 1 \\
\Rightarrow 4n_1 = 4n_2 \\
\Rightarrow n_1 = n_2 $$
and we have demonstrated what is required. Thus, $f$ is $1-1$.
2) Let's see if the function is onto (it might not be). If it is, we should be able to choose any $m \in \mathbb{Z}$ and show that there is some $n \in \mathbb{Z}$ such that $f(n) = 3n - 1 = m$.
If this is true, then we will need $n = \dfrac{1}{3}(m + 1)$. The problem is that this might not be an integer! For example, if $m = 1$, then $n$ would need to be $\dfrac{2}{3}$, which is not an integer. Thus, there is no $n \in \mathbb{Z}$ such that $f(n) = 1$ and so the function is not onto.
EDIT: For fun, let's see if the function in 1) is onto. If so, then for every $m \in \mathbb{N}$, there is $n$ so that $4n + 1 = m$. For basically the same reasons as in part 2), you can argue that this function is not onto.
For a more subtle example, let's examine
3) $f : \mathbb{N} \to \mathbb{N}$ has the rule $f(n) = n + 2$. If it is onto, then, for every natural number $m$, there is an $n$ such that $n + 2 = m$; i.e. that $n = m - 2$. Now, we don't have the same problem as we did before, that is, we don't have to divide by anything to solve for $n$. Thus there is always an integer $n$ so that $n + 2 = m$.
BUT! If $m = 1$ (for example), then $n$ would have to be $1 - 2 = -1$ which is not a natural number, so this function is not onto either.
The point of all this is, we have to look closely at both the domain and codomain to answer these kinds of questions.
Best Answer
You may be misunderstanding the method. It's not $x$, but rather $y$ that's given. You have to construct an $x$ with the property that $f(x)$ yields the given value of $y$. For your example, you can do this if and only if the specified $y$ satisfies $y\geq 0$.
The reason is that if $y\geq0$, then you can note that plugging that same value into the function yields the specified $y$: $f(y) = |y|=y$ (because you already know $y$ is not negative). In other words, you may construct $x=y$. Of course, you could also construct another value (namely, $-y$) which also yields $y$: $f(-y) = |-y|=y$ (because you know that since $y$ is nonnegative, then $-y$ is nonpositive, so its absolute value is its opposite $-(-y)\equiv y$).
This shows that at least all nonnegative values are realized as function values.
On the other hand, you can show that no negative value is realized as a function value in a similar way. If $y<0$, then there can be no $x$ with $|x|=y$ (because the left side is nonnegative but the right side is negative).
So in the end you have shown that the image of $f$ is $[0,\infty)$, so $f$ is "onto" if you take the domain and range as $f:\mathbb R\to [0,\infty)$. If you expand the range, the function will no longer be an "onto" function.
Does this make sense?