Why is my iPhone selecting random areas that i'm not even touching?

It isn't doing it at the moment but sometimes I'll be typing and it will hit the space button by itself and won't stop until I lock and unlock my phone

How do I fix this?

That is probably a digitizer issue. It can't really be fixed, so the display assembly has to be replaced. Depending on the iPhone model, it might cost more than what your phone is worth to replace the screen.

Try wiping the screen with a dry microfibre cloth.

If it doesn't help, at least the attempt was cheap.