Why did the United States shift from expanding internally towards foreign expansion in the late 1800s and early 1900s?
There are at least two reasons for this.
First of all, the United States had pretty much run out of room for "internal" expansion by the late 1800s. The only "internal" expansion that could have happened after that would have been at the expense of Canada or Mexico. The US was never going to get either of those peacefully and it was not going to fight wars against established countries to take parts of their territory at this point in history.
Second, the places that the US took outside the continental US were not major established countries. They were colonies of other countries (Puerto Rico, the Philippines) or very little countries (Hawaii, parts of Samoa). Therefore, they would not get into any trouble with other countries if they took these places. Taking Hawaii was much less controversial in the international community than taking Mexico would have been.