DanielB is not wrong, but your question is more along the lines of "how".
Routers typically are set up to not allow remote administration from outside of your LAN. But they will accept remote administration access from computers within their own LAN.
Your malware probably used your computer to access the admin pages, so the router accepted an admin connection from within the LAN. The malware may have done it entirely from within a browser session, but more likely it just dropped a trojan onto your system that then inspected the router and tried a list of known vulnerabilities (like simply using the default factory password) and then made configuration changes to the DNS to route all your requests through their ad servers where they proxy all your pages and inject code into them.
You should probably do a hard factory reset on it (check the manual, but usually something like: turn off, press and hold the reset button for 90 secs while turning it back on) and then reconfiguring it. Change the password.
Note that it is possible to upload new firmware in this manner, so it is possible it is more than just a user configuration change.
You also need to inspect your computers for any program droppers and/or trojans which will simply re-edit your router settings.