It some sense, it is unfortunate that the first two problems that many students encounter are the free particle and the infinite square well, because both problems are quite subtle from a technical standpoint.
Let's take a look at your questions first:
then they claim that k is a real non-negative number (why? I have no idea)
There's no reason $k$ should be non-negative - DanielSank mentions this thoroughly in his answer. It must, however, be real, and this forces the energy to be non-negative.
On the other hand, when the text go on to solve infinite potential well, they at first glance do something completely different, despite the problem, within the well, is the very same
The problems are not the same - the difference in domain radically alters the nature of the problem. In the case of the infinite square well, the Hamiltonian has a discrete spectrum, and therefore a complete set of eigenvalue/eigenstate pairs, from which one can construct any arbitrary function in the Hilbert space.
On the other hand, in the case of the free particle, the Hamiltonian has a continuous spectrum. This means that it does not have any eigenstates in the Hilbert space, so the above recipe fails spectacularly. It does, on the other hand, yield a set of unphysical quasi-states, which do not live in the Hilbert space, but from which we can construct the physical solutions which do. From there, we can look at the time-evolution of the physical states by examining the time-evolution of the unphysical ones from which it is built.
I will skim over each problem, and give you some context for why one might take slightly different approaches to them. I will be somewhat technical, but hopefully not overly so.
When you write down a problem in quantum mechanics, you need to define a Hamiltonian operator and a Hilbert space of states on which it may act. If the system corresponds to some subset of physical space (say, a particle on a line) then a good guess for the underlying Hilbert space is $L^2(\mathbb R)$, which essentially consists of all functions $f:\mathbb R \rightarrow \mathbb C$ such that
$$\int_{\mathbb R} |f(x)|^2 dx < \infty$$
equipped with the inner product
$$ \langle f,g\rangle = \int_\mathbb R \bar f(x)\cdot g(x) \ dx$$
This is a good choice because we interpret $\int_a^b |f(x)|^2 dx$ as being proportional to the probability that the particle is measured to have a position in the interval $[a,b]$ (with equality holding if $f$ is normalized). We should at the very least demand that a state be normalizable - otherwise this interpretation falls apart.
Once we have decided on the Hilbert space, we need a Hamiltonian operator $\hat H$ which is self-adjoint on our Hilbert space. Notice that $\hat H$ needs to be self-adjoint, not merely Hermitian. Those words are often taken to be synonyms, but they are not. The distinction has to do with the domains on which unbounded operators such as $\hat X,\hat P,$ and $\hat H$ are allowed to act; I won't actually carry out any such calculations, but I'll mention when operators are Hermitian but not self-adjoint.
Anyway, once we decide on a Hamiltonian, we typically seek to find its eigenstates, from which we can build our general solution. This is not always possible (e.g. in the case of a free particle), but it's a reasonable starting point. Let's take a look at the actual problems in your question.
Free Particle
A free particle is just a particle on a line, so our Hilbert space should be $L^2(\mathbb R)$. The momentum operator $\hat P$ operates on a state like this:
$$\hat P \psi := -i\hbar \psi'$$
and the Hamiltonian operator is simply
$$\hat H\psi := \frac{1}{2m}(\hat P \circ \hat P) \psi = -\frac{\hbar^2}{2m} \psi '' $$
We seek eigenfunctions of the Hamiltonian operator - that is, we seek functions $f\in L^2(\mathbb R)$ such that
$$ -\frac{\hbar^2}{2m} f''= E f $$
$$\implies f'' = -\frac{2mE}{\hbar^2} f$$
for some eigenvalue $E$. There are three non-trivial subtleties here:
- An arbitrary element $f\in L^2(\mathbb R)$ is not even continuous, much less twice differentiable, and
- Even if $f$ is twice differentiable, there is no guarantee that $f''\in L^2(\mathbb R)$, and
- The two linearly independent candidates, namely the complex exponentials $e^{\pm ikx}$, are not even in $L^2(\mathbb R)$ themselves
The first two issues can be swept under the rug by only considering functions $f\in L^2(\mathbb R)$ which are twice differentiable, and whose second derivatives are also in $L^2(\mathbb R)$ (the set of such functions is called the Hilbert-Sobolev space $\mathcal H^2(\mathbb R)$).
The third issue is more problematic. The typical way to handle it is to treat the complex exponentials as "quasi-states" which are unphysical, but still satisfy the above differential equation. If $f=\exp(ikx)$, then
$$f'' = -k^2 f = -\frac{2mE}{\hbar^2}f$$
We then ask whether we could build a state in $L^2(\mathbb R)$ out of such states. We can, and the result is a Fourier transform:
$$ f = \int_{\mathbb R} g(k) e^{-ikx} dk $$
From Parseval's theorem,
$$\int_{\mathbb{R}} |f|^2 \ dx = \int_{\mathbb R} |g|^2 \ dk$$
So as long as $\int_{\mathbb R}|g|^2 \ dk < \infty$ then we are guaranteed that the resulting $f\in L^2(\mathbb R)$, and therefore corresponds to a physical state.
The time dependent state is then
$$ f(x,t) = U(t,0) f(x,0) = e^{-i\hat H t} \int_{\mathbb R} g(k) e^{-ikx} dk$$
$$ = \int_{\mathbb R} g(k) e^{-ikx} e^{-i \frac{\hbar^2 k^2}{2m} t} \ dk$$
Notes:
- The plane waves $e^{-ikx}$ are not solutions to the time-independent Schrodinger equation, because the TISE is an eigenvalue equation on the Hilbert space $L^2(\mathbb R)$, and the complex exponentials do not belong to that space.
- Even so, we can build elements of $L^2(\mathbb R)$ from these unphysical quasi-states via superposition, which takes the form of a Fourier transform over momentum space; the resulting physical states evolve just like a superposition of the unphysical ones.
- The TISE does not have any eigenfunctions or eigenvalues in $L^2(\mathbb R)$, which means there is no such thing as physical states with definite energy for this system.
The last point is crucial and often slightly overlooked. We often claim that self-adjoint operators have complete sets of eigenstates, but this is generally not true if the spectrum of the operator is continuous, as it is here.
As an aside - it's easy to show that $\hat H$ is Hermitian, but somewhat tricky to show that it self-adjoint, which is a stronger requirement. The same is true of $\hat P$ - it is self-adjoint (and therefore Hermitian), but it has a continuous spectrum, and so it has no physical eigenstates (though as before, we can treat the complex exponentials as quasi-states of definite momentum).
Infinite Square Well
In this problem, the particle is artificially restricted to lie in the interval $I=[0,L]$. The Hilbert space $L^2(\mathbb R)$ is no longer an acceptable choice, so we make the modification that the Hilbert space underlying our system is now $L^2(I)$.
We actually need to make another restriction - we demand that physical states $\psi$ obey the boundary conditions $\psi(0)=\psi(L)=0$. Therefore, our true Hilbert space $h$ (I'm sorry, we're running out of h's) is given by
$$h :=\{\psi \in L^2(I) \big| \psi(0)=\psi(L)=0\}$$
As before, we take the Hamiltonian operator to be
$$ \hat H \psi := -\frac{\hbar^2}{2m} \psi ''$$
which we permit to act on functions which live in the domain $\mathcal D_H$, where
$$\mathcal D_H := \{\psi \in \mathcal H^2(I) \big| \psi(0)=\psi(L)=0\}$$
Unlike the previous case, the TISE actually yields solutions which belong to our Hilbert space, and which take the form
$$\psi_n = c_n \sin\big(\frac{n\pi x}{L}\big)$$
with eigenvalues
$$E_n = \frac{n^2\pi^2\hbar^2}{2mL^2}$$
Notes:
- As before, the Hamiltonian operator is self-adjoint on this Hilbert space. However, since the spectrum of $\hat H$ is purely discrete, it does have a complete spanning set of eigenstates, as well as a notion of physical states of definite energy.
- One might imagine that the same is true of $\hat P$. However, it can be shown that $\hat P$ is not self-adjoint on this Hilbert space. Therefore, not only is there no such thing as a state of definite momentum, there isn't even a well-defined notion of what momentum is.
Note that $\hat P$ as defined above actually takes elements of $h$ out of the Hilbert space. This is due to our extremely restrictive boundary condition that $\psi(0)=\psi(L)=0$. If we loosen it to periodic boundary conditions, so that $\psi(0)=\psi(L)$ but they are not necessarily zero, then it turns out that we can create a self-adjoint momentum operator. This corresponds to a particle on a ring, and is an important problem to look at later.