How do companies know they've been hacked?

Solution 1:

Generally they look for subtle forensic clues; such as their homepage being changed to a banner which reads "p0Wned by TeH L33t Krew!! haahah1h1!! u noobs"

Solution 2:

A successful hack is one that goes undetected ;)

A sysadmin can set up honey pots, dummy computers to fool hackers into thinking that they're a real system with real data. In the honey pot all activity is monitored and the behaviour of the hacker is studied to help learn more about what a hacker or virus is trying to do to help security experts figure out how to prevent future intrusions.

They can also use automatic Intrusion detection systems to help them detect suspicious activity