If the car slips out of gear, and the parking brake doesn't hold the car, you want to avoid having the car roll out into the street.
Let's separate uphill and downhill, and it might make more sense.
If you're parking uphill, and there is a curb, turning the wheel away from the curb means that the car will start to roll backwards, the front end will swing to the right, and the right front tire will bump up against the curb and stop the car. But that won't work if there is no curb-the car will just back out into traffic, because there is no curb to stop it. In that case, you want to turn the wheel toward the side of the road, so the car will just roll off the road.
Parking downhill, you want to always turn the wheels toward the curb, or the edge of the road if there is no curve. If the car starts to roll, the front end will swing right again. The tire will hit the curb, if there is one, and stop the car. If there's no curb, the car will just roll off the road.
It helps if you can picture what the car would do if it started to roll.