One of the fundamental tasks of toddlerhood is to experiment with independence. That's why they say "No!" a lot, and why they take great glee in wandering off on their own. The problem, as you said, is that they don't understand the dangers of some of their behaviors.
The key to preventing problems is using appropriate discipline. For a child your son's age, that means environmental control, not warnings or punishments. While it's OK for your son to run freely in a playground, in your backyard, or inside your home, the rules are different when he's in a parking lot, in a shopping mall, or on the sidewalk. During those times he must hold an adult's hand or sit in a stroller.
When my son was this age, he also would try to assert his independence by running off. I would hold his hand and, when I felt him trying to squirm out of my grip, I'd hold it tighter - being careful not to hurt him, of course. He quickly learned that there were times when he was free to explore and times when he was not. If it was going to be a long trip, I'd have him sit in his stroller so that we wouldn't have an extended battle of wills.
Another option that many parents like is a safety harness that the child can wear that's attached to the parent by a tether. (Some parents are upset by this because it reminds them of keeping a dog on a leash.) This can be very effective, although you still have to keep a constant eye on your child. I advise against the tethers that go around a child's wrist since they can lead to injury.
In any event, remember that this type of impulsive running away will soon disappear as your child matures.