How do shared pointers know how many pointers point to that object? (shared_ptr, in this case)
They hold an internal reference count that is incremented in the shared_ptr copy constructor/assignment operator and decremented in the destructor. When the count reaches zero, the held pointer is deleted.
Here's the Boost library documentation for smart pointers. I think the TR1 implementation is mostly the same as boost::shared_ptr.