The answer by @arivero is perfect but I would like to present a bit different argument than the standard--the one which plays out in terms of the boundary condition. Of course, my arguments can (almost trivially) be translated into the boundary condition language.
I had plugged the wave function into the time-independent Schrodinger equation for a particle in a box. By solving the Schrodinger equation from both sides, I saw that the left-hand side equaled to the right-hand side, hence the function is a solution to the Schrodinger equation.
Well, you didn't really see that the two sides are equal--or rather, you shouldn't have seen that the two sides are equal!
Here is why: The time-independent Schrodinger equation needs to be satisfied for all values of $x$. What you essentially did was to verify that $\psi=A\cos(kx)$ solves the time-independent Schrodinger equation for a potential $V(x)=0$. But that is not really the particle-in-a-box problem--that is just the free-particle problem. And $ \psi = A \cos(kx) $ is a solution to that problem--no doubt! The potential for the particle-in-a-box problem is the following $$V(x)=0 \text{ for }x\neq0,L$$
$$V(x)=\infty\text{ for }x=0,L$$
So, in order to really solve the particle-in-a-box problem, we need to make sure that the time-independent Schrodinger equation is solved by our solution for this $V(x)$ which distinctively differs from the free-particle potential at $x=0,L$. Now, since $\psi = A \cos(kx)$ solves the time-independent Schrodinger equation for the free-particle problem, it does solve the particle-in-a-box problem as well for all points $x\neq 0, L$, But we must also take care of $x=0,L$. So, let's ask the question as to whether \psi = A \cos(kx) satisfies the time-independent Schrodinger equation at $x=0,L$ or not.
The time-independent Schrodinger equation reads $$-\dfrac{\hbar^2}{2m}\dfrac{d^2}{dx^2}\psi(x)+V(x)\psi(x)=E\psi(x)$$
Now, if we put in $\psi = A \cos(kx)$ then for it to be the solution, the following should necessarily hold true
$$\dfrac{\hbar^2k^2}{2m}\cos{0}+V(0)\cos{0}=E\cos{0}$$
Or, equivalently,
$$V(0)=E-\dfrac{\hbar^2k^2}{2m}=0$$
There is no way for this to be true since $V(0)=\infty$ and thus, our proposed solution is simply wrong! In particular, it fails to satisfy the time-independent Schrodinger equation at $x=0$.