Since the particle cannot with any probability be outside the box, the wave function there must be zero, so by continuity that boundary condition holds.
The wavefunction $\psi(x) = \frac{1}{\sqrt{L}}$, which results in a uniform spatial probability density, is perfectly allowed for the particle-in-a-box, whose Hilbert space is indeed $L^2\big([0,L]\big)$. The boundary conditions $\psi(0)=\psi(L)=0$ are not (or rather, need not be) restrictions on the Hilbert space, they're restrictions on the domain of the Hamiltonian.
That is, the Hamiltonian operator is a linear map $\hat H : \mathcal D(\hat H)\mapsto L^2\big([0,L]\big)$, where
$$\mathcal D(\hat H) := \bigg\{\psi \in L^2\big([0,L]\big) \ \bigg| \ \psi\text{ is twice (weakly) differentiable and }\psi(0)=\psi(L)=0\bigg\}$$
$$\hat H \psi = -\frac{\hbar^2}{2m} \psi''$$
This is exactly the same problem, except we lack the boundary conditions: since there is no zero wave function outside the box to speak of (because the outside doesn't exist), we cannot argue that the wave function goes to zero on the boundary.
Without boundary conditions, this Hamiltonian is not Hermitian (check!). One possible choice of boundary conditions is $\psi(0)=\psi(L)=0$; this defines the particle on a box. On the other hand, periodic boundary conditions $\psi(0)=\psi(L)$ and $\psi'(0)=\psi'(L)$ would yield a perfectly well-defined (and Hermitian) Hamiltonian, which would correspond to a particle on a ring.
For an infinite line with infinite walls, QM is in principle defined on the whole space. However, the wave function is zero everywhere in the potential, and on the edges it needs to go to zero by continuity.
The way to say this is that
$$\mathcal H := \bigg\{\psi\in L^2(\mathbb R) \ \bigg| \ \psi(x)=0\text{ for } x\notin [0,L]\bigg\}$$
constitutes a Hilbert space$^\dagger$. We are then free to choose the (self-adjoint) Hamiltonian $\hat H:\mathcal D(\hat H) \rightarrow \mathcal H$, where
$$D(\hat H) := \bigg\{\psi \in \mathcal H \ \bigg| \ \psi\text{ is twice (weakly) differentiable}\bigg\}$$
$$\hat H \psi = -\frac{\hbar^2}{2m}\psi''$$
Doing so yields two results:
- The requirement of differentiability for $\mathcal D(\hat H)$ implies continuity, which implies that $\psi(0)=\psi(L)=0$. Note that this is true only for those vectors in $\mathcal D(\hat H)$, because arbitrary vectors need not satisfy the differentiability requirements.
- $\hat H$ is Hermitian, because $\psi(\pm \infty) = 0$ by the definition of the Hilbert space we're working in.
When we work in the big picture (i.e. the interval is the whole universe), there are no a priori boundary conditions. We need to bring boundary conditions (which are arbitrary) or the system is ill-defined. Is that right?
There are no a priori boundary conditions on the domain of the Hamiltonian, yes. On the space $L^2\big([0,L]\big)$, you will find that the free-particle Hamiltonian is not Hermitian unless you suitably restrict its domain with boundary conditions. Again, though, I must emphasize that these boundary conditions do not apply to the whole Hilbert space, but rather only those elements of the Hilbert space that $\hat H$ is allowed to act on.
$^\dagger$There is a bit of subtlety related to the fact that $L^2(\mathbb R)$ consists not of functions but rather of equivalence classes of functions - see e.g. here - but this ends up not being problematic for the current discussion.