How does the toothpaste end up back in the tube?

This phenomena is generally observed when toothpaste is partially empty tubes but not in fully tubes.

And I'd bet good money that the phenomenon is only observed with plastic tubes and not with metal (typically aluminium) ones.

Note that when you squeeze a soft and hollow plastic object (think bottle or drinking cup) and you subsequently release the deforming force, the object usually returns back to its original shape (or at least partly).

But a thickish aluminium foil will not: its deformation will be mostly permanent.

Now bear in mind that outside our plastic toothpaste tube pressure is $1$ $\text{atm}$.

Both factors combined, the resilience of the plastic tube and the outside pressure, work together to push the paste back into the tube (at least partly).

The toothpaste tube material has some small bit of springiness to it so that when it is squeezed there is a tendency to return to the original shape - like a spring. Unlike the spring which tries to return to it's original shape, the tube has very poor elasticity so it only moves a little to return it to its original shape. Inside the tube is the toothpaste which is water based and it is not compressible in this situation. When the tube is squeezed, the toothpaste comes out. Once the tube is released and tries to return back to some fraction of its original volume, drawing in the toothpaste or air. This occurs because of the atmospheric air pressure which is pushing air or toothpaste back into the tube to prevent a vacuum from occurring.