In your "pseudo-code proof", you left out an essential bit. We try to prove that there is no machine H, which for every input x, y halts and outputs "yes" if the machine x halts with input y, and halts and outputs "no" if the machine x doesn't halt with input y.
We do a proof by contradiction: We assume that H exists, and we show this leads to a contradiction. And because the assumption that h exists leads to a contradiction, the assumption must be false, so we proved that H didn't exist.
So the question "How does H know whether x halts on x?" doesn't make sense. We assume that H correctly decides whether x halts on x. We have no idea how H might do that. Actually, we want to prove that H cannot do this for every x. But for our indirect proof, we assume H can do this.
Next you say "H must emulate x". Well, emulating a turing machine to see whether it halts or not won't let you decide if the turing machine doesn't halt because the emulation won't ever finish. So that's not a good approach. Whatever H does, it's not going to emulate x. It must do something clever. As intelligent humans, we can use intelligence to decide for many x, y whether program x halts with input y. Sometimes we fail. But that might be because of lack of intelligence, not a problem with the input. We could imagine that there is a H that is intelligent enough to make the correct decision for every x, y.
So now we run H(x,x) where x is the encoding of P.
Due to our assumption, we know that H (x, x) halts, and it returns "yes" if P(x) halts and "no" if P(x) doesn't halt. How do we know this? Because that's our assumption that we use for the contradiction proof.
Does P(x) halt or not halt? It either halts or it doesn't.
If P(x) halts, then it first calls H(x,x). H(x, x) figures out correctly that P(x) halts, so H(x,x) outputs "yes", P(x) detects that H(x,x) did output "yes", so P(x) loops forever and doesn't halt. There we have a contradiction.
If P(x) doesn't halt, then it first calls H(x,x). H(x, x) figures out correctly that P(x) doesn't halt, so H(x,x) outputs "no" and then halts, P(x) detects that H(x,x) did output "no", not "yes", so P(x) stops immediately and halts. There we have again a contradiction.
So both "P(x) halts" and "P(x) doesn't halt" lead to a contradiction, therefore our assumption is wrong, therefore H doesn't exist. Bingo.