Asteroids are the left over bits and pieces from the solar system’s early days that didn’t quite make it to being a planet, for one reason or another.
Most planets formed relatively early in the solar system when a given lump of material (called a planetestimal) started crashing into nearby lumps of dust and rock with just enough force to get the other pieces of rock to stick to it, without having the smaller object simply bouncing off or shattering. This method of accretion works until the planetesimal gains enough mass to start attracting objects through gravitational forces, which can help it gain mass even more quickly. Once a particular lump reaches a certain mass, its own gravitational weight will start making it rounder and rounder. For some objects, this process will continue, until they form a fully-fledged planet.
This ideal case of catching smaller lumps of material is rarely a smooth process. In the earliest stages of building up a planetesimal, the rate at which it can gain mass is largely dependent on how much nearby matter there is to be wafted in the right direction to clump on to our object. Ultimately, this means that some planetesimals will grow more rapidly than others, if they find themselves in a region that has a lot of material with which to grow. In turn, this means that the early solar system was a very hectic place, with swarms of little planetesimals of various sizes. Some of these planetesimals will unavoidably be absorbed into one of its larger neighbors, if it doesn’t suffer a more catastrophic fate.
We mentioned earlier that to get a smaller object to stick to you and gain its mass, you have to run into it with the right amount of force. If you run into it with too much force, you can shatter the smaller object into small fragments. There are two main ways to have too much force in your collision- high velocity and large mass. If you’re a much more massive object, you’re likely to just shatter the small object. But if you’re both small objects, and you hit at high speeds, both of you will shatter. (This is what we think happened to create that X-shaped asteroid Hubble spotted a couple years ago.) What’s left over - these shards of former planetesimals - can then continue to wander the solar system in places like the main asteroid belt between Mars and Jupiter, or out beyond Neptune.
There are a few objects that seem to have done okay in building up mass to the point of making themselves round, and also managed to avoid being destroyed in one of these collisions, but then failed to continue gaining mass to the point where there weren’t any other objects in their orbit - these are what we now call dwarf planets. Like brown dwarf stars, they made it partway to being a planet, but didn’t quite make it all the way there.
Something here unclear, or have your own question? Feel free to ask!