Similar Questions for How Does Bgp Prevent Routing Loops from Yahoo AnswersQuestionAnswer
Your question should really specifiy external or internal BGP, since they differ. The short answer is the AS_PATH attribute, but there are exceptions.
As a route crosses between autonomous systems, the AS number of the transmitting router is added to the AS_PATH attribute. If a router sees its own AS number in that path list, then it considers it to be a routing loop and ignores it.
Now, often you'll want to advertise an aggregate route, combining multiple smaller subnets into a single advertisement. This creates problems with the above scenario, since those different smaller subnets might have different AS_PATH information. So, in some cases BGP will use the AS_SET attribute for loop detection (which is just an unordered AS_PATH list combining those of the smaller subnets in the aggregated route).
Another issue is internal BGP, since all routers inside the same AS are naturally going to have routes coming in from the same AS (since that's kind of the point). To prevent loops here, it gets a little complicated but basically there are restrictive rules on which internal BGP neighbors are allowed to distribute routes learned through internal BGP.
Routes to Null0 are used in general (not just in BGP) to prevent routing loops when addresses are being summarized. When route summarization takes place, the router could get traffic for any subnet within the summarized range, and not all subnet ranges might be in use, so the default routes could loop the packets in a count-to-infinity scenario. Here is a reference link with a configuration example: