You're just beginning your study of quantum mechanics, so I would advise you to be careful not to try to interpret quantum mechanics through the lens of classical mechanics. It's a very reasonable thing to imagine quantum tunneling as a little ball which magically pops through a barrier and emerges on the other side, but that is an outstanding way to develop bad intuition which you'll need to fix down the line. Quantum mechanics is fundamentally different from classical mechanics, and it is the latter which should be understood as a limiting case of the former, not the other way around. In that sense, the real question should be not why quantum particles can tunnel, but why classical particles (whatever that means) apparently cannot.
With that being said, the rough idea is the following. We can gain some useful intuition by studying the simpler case of what happens when a particle encounters a potential step of the form
$$V(x) = \begin{cases} 0 & x<0 \\V_0 & x\geq 0\end{cases}$$
and then extend this to a potential barrier of width $L$, because the latter is just a step up followed by a step down.
The (generalized) eigenstate corresponding to a particle incident on the barrier from the left with energy $E=\hbar^2k^2/2m<V_0$ takes the form
$$\psi_k(x) = \begin{cases} e^{ikx} + r_k e^{-ikx} & x < 0 \\ t_k e^{-q_k x} & x \geq 0\end{cases}$$
where $$\matrix{q_k \equiv \sqrt{\frac{2m(V_0 - E)}{\hbar^2}} \\ r_k \equiv \frac{2iq_k}{k-iq_k}\\ t_k \equiv 1+r_k = \frac{k+iq_k}{k-iq_k}}$$
Based on this picture, we might imagine (correctly) that there is a nonzero probability of measuring a particle with $E<V_0$ within the potential step. However, we need to be a bit careful - this is a non-normalizable (and hence unphysical) state, after all, so if we want to understand what happens dynamically, we should construct a real, physical state. Such states take the form of wavepackets, which may be written
$$\Psi(x,t) = \frac{1}{\sqrt{2\pi}}\int \mathrm dk \ A(k) \psi_k(x) e^{-iE_kt/\hbar}$$
for some square-integrable function $A(k)$ (where $E_k \equiv \hbar^2 k^2/2m$). In essence, $A(k)$ tells us how much of the state with energy $E_k$ is present in the wavepacket. The take-away is that real states consist of an integral superposition of energy eigenstates, not specific energies, and if we want to understand what happens dynamically when a particle encounters a potential step, we need to consider what happens to one of these wavepackets.
The specifics of this are actually rarely covered in detail because while the process is conceptually fairly simple, the calculations are tedious and need to be performed numerically. The qualitative picture goes like this:
- The components of the wavepacket with energy $E>V_0$ are partially reflected and partially transmitted. The transmitted parts propagate forever in the $+x$ direction.
- The components of the wavepacket with energy $E<V_0$ are all reflected eventually; however, they penetrate into the barrier by an exponentially small distance ($\psi_k\sim e^{-x/\ell_k}$, where $\ell_k=1/q_k$) and are delayed by a correspondingly small amount of time before being reflected.
In particular, if all of the components of the wavepacket have energy less than $V_0$, then the wavepacket will be perfectly reflected - however, it will be distorted because the different components penetrate different depths into the step before being reflected, and during the reflection there will be a nonzero (but exponentially small) chance of measuring the particle to be physically located at some $x>0$.
We can now turn our attention to your main question of what happens when we have a potential barrier of width $L$, and a wavepacket whose components all have energy less than $V_0$. From a qualitative and dynamic perspective, everything proceeds exactly as it did with the potential step. As the wavepacket approaches the barrier, its components penetrate into the classically forbidden region by an exponentially small distance before being reflected. However, because the barrier has a finite width $L$, a fraction $\sim e^{-L/\ell_k}\equiv e^{-q_k L}$ of the components of the wavepacket will make it all the way through the barrier and escape to the other side$^\dagger$.
You can find an animation of such a process here. Note that the mean energy of the wavepacket in this simulation is much lower than $V_0$, and so essentially none of the wavepacket is able to reach the far end of the barrier. However, observe the exponentially-suppressed penetration of the wavepacket into the front side of the barrier, and then imagine what would happen if the barrier were significantly thinner so the wave amplitude at the back edge was not effectively zero.
How can the object "know" that across the wall there's going to be a lower energy and, thus, the borrowed energy will be restored and not depleted.
I think the "borrowing energy" metaphor is not really a good way to think about it, for essentially the reason you mention. The particle doesn't need to know that the barrier has finite width; the penetration of the wavepacket into the barrier proceeds the same way in both cases, but if the barrier is not infinitely long then an exponentially small fraction of the wavepacket will reach the back edge and escape.
$^\dagger$In fact, this is an oversimplification. In reality, the components of the wavepacket which reach the back edge of the potential are not perfectly transmitted - some of them reflect backward into the barrier, so the precise expression for the tunneling amplitude is a bit more subtle than simply calculating $e^{-q_k L}$ (though that does provide the right order of magnitude).
Remark on Localization
(My initial reading of the question was sloppy, and I thought OP was asking about a potential step rather than a potential barrier. As a result, this is no longer particularly relevant, but it is mildly interesting, so I elected to include it as an afterthought.)
As an interesting side note, it turns out that a particle which is initially localized to some compact interval $[x_1,x_2]$ to the left of the barrier (by which I mean, $\psi_0(x)=0$ for all $x\notin[x_1,x_2]$), then the wavepacket must contain components with energy $E>V_0$. This is related to a well-known theorem about Fourier transforms which says that a function and its Fourier transform cannot both be compactly-supported; in this context, the interpretation is that the better-localized you want your initial particle to be, the more high-energy components you will need to include in the wavepacket.
As a result, a wavepacket with average energy $E<V_0$ which is initially localized to a compact interval $[x_1,x_2]$ will always be partially transmitted, even through an infinitely long potential step, because it will contain some high-energy components which exceed the barrier height. Of course, even more of such a wavepacket would be transmitted through a potential barrier of width $L$, because the high-energy components would be partially transmitted and an exponentially small fraction of the low-energy components would be able to tunnel.