There is a difference between autofocus when using the viewfinder and when using LiveView.
In LiveView, the image sensor is acquiring the correct focus whereas there is a separate focusing sensor that is used when using the Viewfinder.
The LiveView is usually most accurate, as it has exactly the same distance to the area that the image is being projected to as the sensor itself (obviously, as it IS the sensor doing the focusing). But when using the separate focusing sensor, there could be a minor difference between the distance of the projected image on the sensor and on the focusing sensor.
That is why many photographers are Autofocus Micro Adjusting their lenses.
I don't think that is possible on a Canon EOS Rebel T5 camera though.
But reading your question, I wonder if the problem really is that the focusing sensor does not receive enough light to acquire focus? again, LiveView uses a different technology and it is possible that it struggles less in the same light.
You say that is focuses in and out, but will not let you take the focus. That sounds like the lens is focus hunting and then gives up. This will usually occur in low light or on subjects that have low contrast (e.g. being mostly a uniform black surface).
Shooting in better light, or with a lens that lets in more light can remedy this.