There are two parts to this. The first is easy. If you are tenured then you just make the change, either immediately or gradually. Tenure gives you the freedom to do that. If you are untenured then you make the change and worry a lot about whether you will get tenured.
There is, however, one scenario in which it is advantageous for even an untenured person to change. If you are the new hire in a department in which there is no one to collaborate with then it might be an advantage, even if you are untenured, to join a research group and move toward their interests. It could, perhaps, enhance tenure possibilities.
The second part is a bit more complex, assuming you want to behave ethically. You can't really advise students very well if you have no knowledge, though a surprising amount can be accomplished. It is, in theory, possible to teach things that you don't know yourself, but you have to do it carefully, putting the student on a learning path that you haven't followed yourself. This is possible if you know about research and how to conduct it and are sophisticated enough to follow the arguments put forth by the student, just as you would the arguments of any colleague. I'll note that research groups actually do this - exploring a path that no one has followed.
But if you change fields gradually then you can also work into the advising role gradually, perhaps by being a co-advisor of a few students - even if informally until you get the required background.
I'll note that advising students outside your expertise, while possible, is risky and so the methodology used needs to remove risk for the student. A student, on the other hand, is best advised to choose an advisor who already has sufficient expertise in the direction to be followed in the research.
Note also that at the end of doctoral research the student is actually expected to know more about the specific research area than the advisor, unless the student has brought the advisor along on the journey to the end. If the opposite were true we would never advance, with each student being somehow less than the advisor.
Note that I've assumed here that the change is to a field that has similar research methodologies, such as changing subfields within a larger field. It is harder otherwise, of course. But the way we do mathematics or computer science is pretty similar within most subfields. Similar for chemistry, say. But to switch from math to history is a bigger challenge as the research methodologies are (I assume) much different.