Pilots today have more than enough technology both in the cockpit and in the infrastructure that supports them to know with great precision where they are at any given time. How/why do they still sometimes go off-course?
1) Technology is fallible.
2) Humans are fallible.
Now that aircraft have GPS receivers it is rare for them to be off course by a considerable margin. That said, technology can fail and there are cases where the GPS cannot, or should not, be used.
The crew can also make mistakes, either by incorrectly reading the navigation data or by incorrectly entering flight path details into the navigation system: An aircraft may actually be 'on course' based on what the crew told it to do, but they told it the wrong thing. Depending on how data is entered into the system it's easy to add the wrong waypoint (some have very similar names, there's been a few cases of that happening) or to enter reciprocal bearings or lat/lon points. On top of that there's simple crew distraction - if they're manually flying then they may be focussing on something else and not notice that are off-course: As happened on the Sukhoi superjet demonstration flight a few years ago.
The navigation data (angles, coordinates) are initially numbers and must be read by human first to use the navigation systems. If the pilot misreads or wrongly interprets the initial data, the navigation tools cannot help.
This seems the reason of both Varig Flight 254 and TWA Flight 3 navigation mistakes. In general cases seem rare as they require the navigators to stay disoriented for a long time, regardless of that they see, radio stations they hear, etc, etc.
My Dad was a fighter pilot in the Korean War. He acidentally did something in the cockpit which effected his air mix. Without sufficient oxygen, my Dad got lightheaded and started flying towards North Korea. My Dad might well have died or become a POW except that members of his fighter group flew back to find him. My Dad was sufficiently cognitively aware to recognize his comrades and use them as his "navigators" back to home base.
Things that can effect a pilot's cognitive state can still happen. Look at naval and air force fighter crashes over the past handful of years. As a chemist, I'd identify one likely culprit as new cockpit materials insufficiently tested for offgassing (changed chemical state) at critical state points. I have one material, in particular, in mind.
A basic expansion of os1’s answer.
1) broken or defective equipment.
2) equipment that is not properly maintained or calibrated.
3) improper use of equipment by flight crew.
4) incorrectly interpretation of output data from equipment by flight crew.
5) terrestrial or satellite navaids needed to operate equipment either down for maintenance or unavailable.
6) incorrect procedures used by flight crew.
7) Overconfidence in onboard systems causing negligence of basic airmanship and good risk management.
8) Confusion among duties for pilots in aircraft requiring more than one flight crewmember.
9) saturation by existing cockpit tasks causing negligence in navigation.
10) human frailty, fatigue and external pressures.
It can happen to anybody, in any kind of airplane, no matter how advanced the equipment on board is. I wrote in a post about Instrument flying that modern integrated flightdecks i.e. glass cockpit have the capability to make a good pilot better and a poor pilot dangerous. This still rings true today. Smart people routinely do stupid things in aircraft and it can have tragic consequences. This can be insidious if one is not careful.