I'll preface my answer with the comment that it was a different time and the way that experimental animals, even primates, were treated was different than today.
tl;dr the chimpanzee was trained to press levers upon the illumination of light signals to avoid receiving electrical shocks.
Subject 61 (aka Ham for Holloman Aerospace Medicine) got 29 training sessions prior to launch. The spacecraft console looked like this:
Plates were strapped to the chimpanzee's feet that delivered a "mild electrical shock".
The ballistic unit presented two shock-avoidance programs. The primate had to depress levers in response to colored light stimuli within
a certain time period to avoid a mild shock.
Each response by the subject on the right-hand lever postponed the
occurrence of the next scheduled shock for 15 seconds and, as a
consequence, a consistent and stable rate of responding was
developed by which the animal could avoid shocks indefinitely.
In contrast to the continuous task, discrete avoidance employed a signal as a warning of impending shock if the correct response was not made. In the MR-2 flight, the warning signal was the illumination of the blue light. This light came on once every 2 minutes. However, a fixed time interval was not employed during training in order to eliminate the possibility of temporal conditioning. The time between the appearance of the blue light and the pressing of the left lever was the subject's reaction time. During the flight the subject had to press the right lever at least once every 15 seconds and at the same time press the left lever within 5 seconds after each presentation of the blue light.
The excellent performance of the chimpanzee on the two required tasks
was gratifying. Of the two shocks received during the flight, only one
The undeserved shock (bad form, experimenters!) was explained thusly:
...it is believed that this shock was due to a malfunction in the
timing apparatus since careful examination of the telemetry
recording shows that the time between responses at this period of the
flight was less than 15 seconds.
The orbital space chimp, Enos, had a more complicated console that could dispense banana flavored pellets and water (reward) as well as shocking him (punishment). That's all covered in the source document as well.
Source: Results of the Project Mercury Ballistic and Orbital Chimpanzee Flights
Bonus fact: This mission had an in-flight abort! Ham's escape tower fired, subjecting him to 17 g's.