I’m wondering how well professors know whether they will get tenure or not, and if that will affect their decision to hire a new student? I figure that if a professor needs new students they wouldn’t just stop hiring students because they aren’t sure if they be able to continue at their institution, but then I’m not sure what would happen to the students should the professor be asked to leave the institution after their time as an assistant professor is up.
The simple answer to your direct question is yes and no. Some will and some will not.
But the problem is probably less than it seems for a variety of reasons, not all of which apply in any given case. Some universities tenure people only rarely so most assume that they will be moving on. Some universities tenure most of their candidates, being especially careful when first hiring. In the first case, a professor probably won't want to hire and in the second, probably will.
Moreover, if you are denied tenure, you still have a year before you leave. If the decision is made in the 7th year, you leave at the end of the 8th, not immediately.
In addition, in places that make more individualized decisions, it is likely that the candidate has had quite a lot of advance notice that their application is in trouble. And the reasons for that imply that such a person isn't likely to be the first choice of students looking for a supervisor. Either they have evidenced poor teaching or inadequate research. So a person headed for the door isn't the one you are interested in supervising you in the first place. The person likely to be tenured is also more likely to be the one you would consider as a supervisor.
But, there are exceptions. Some people fail to earn tenure for purely economic reasons when interest in a field wanes as it periodically does in some fields. The person will need to leave, but with good prospects elsewhere through letters explaining why. But even in this situation, there are fairly clear warning signs that the person is leaving as the trends take a while to develop.