OK I found this answer:
All redirections (including >) are applied before executing the actual command. In other words, your shell first tries to open /etc/php5/apache2/php.ini for writing using your account, then runs a completely useless sudo cat.
here: Redirection
EDIT
Based on @Milliways answer, I have applied the following to a script I have written. This code creates a file in the home folder, then moves it to the required folder. Ownership and permissions are set to suit.
#Create the file in the home dir
cat > ~/12-ap0.network <<EOF
[Match]
Name=ap0
[Network]
Address="$ap0_IP"
DHCPServer=yes
IPForward=no
EOF
#move it to where it needs to be
sudo mv ~/12-ap0.network /etc/systemd/network/12-ap0.network
#set the ownership and permissions
sudo chown root:root /etc/systemd/network/12-ap0.network
sudo chmod 777 /etc/systemd/network/12-ap0.network
This adds just one extra line (mv ...) to the code.