Any person can be prosecuted for treason should there be evidence of it. However, if you try to prosecute a president or a judge while in office, the office itself acts as a protection since there are priveleges and connections that go along with it. That’s why it’s proper to remove an official from office and then prosecute.
For example, the president controlse the attorney general’s office and could influence the prosecutor, etc. The president could try to pardon himself. Out of office, these powers are removed. Likewise with a judge.
If he’s not impeached though, can he still be prosecuted for treason later on after he leaves office?
State law enforcement is supposed to be independent of the POTUS, though, so state prosecution wouldn’t/shouldn’t be affected by either impeachment or non-impeachment.