Why does wget give an error when executed with sudo, but works fine without?

You have a proxy defined in your environment. Yours appears to be 127.0.0.1:3128.

When you run sudo, the proxy environment variable isn't passed, which is why you can't directly resolve google.com.

You can see what proxy/proxies you have defined in your environment variables with this command:

env | grep proxy

Additional information on Ask Ubuntu

Note: If you want sudo to pass the HTTP proxy environment variable, try this:

sudo http_proxy="$http_proxy" wget -q --tries=10 --timeout=20 --spider http://google.com

You can also pass all environment variables using sudo -E:

sudo -E wget -q --tries=10 --timeout=20 --spider http://google.com

Stack Overflow has other options for keeping the environment variable when sudoing.