Green’s Dictionary of Slang

fuck with v.

[lit. and fig. uses of fuck v. (1)]

1. [1940s+] to mess about with, to become involved with.

2. [1940s+] to interfere, often by physical or mental intimidation.

3. [1960s+] to copulate with (a partner is usu. cited).

4. [1960s+] (prig. US black) to impress, to overwhelm, to manipulate.

5. [1960s+] to play around with.

6. [1980s] to associate with.

In phrases

fuck with someone’s head (v.)

[1970s+] to disturb and harm someone emotionally.

fuck (with) someone’s mind (v.) (also fuck someone’s head)

[1960s+] to intimidate, astonish or confuse another or oneself.