At every game, there are two groups of two people who are in charge of collecting these stats. One person in each group keeps an eye on the game and tells the other person what events to mark down. The other person has a computer tablet with which they mark these things down--so player A just got a rebound...player B just attempted a shot and missed, etc. The referee's whistle actually activates the devices to know when to start and stop the game clock.
The information input to these tablets is sent to the official scorer and any calculations are automated (such as field goal percentage). The reason there are two teams doing this is for times where one person may miss something or the two groups' info doesn't match. In those cases, they can use a DVR to replay the event in question.
For newer, more advanced statistics, this is pretty cool:
Six cameras are set up to continuously track every movement and output some interesting stuff.