Watch an NFL quarterback throw a football to a receiver and you'll notice he imparts a spin on the football as he throws it. A football thrown with a lot of spin will fly smoothly in the air and hit its intended target. On the other hand, if he doesn't put a spin on the ball, it will wobble in the air and tend to move in a random direction and is much harder to catch. It is the same principle.
The difference between a smoothbore weapon such as a musket and a rifle is the presence of rifling (i.e. the grooves) cut into the barrel. This is what gives the rifle its name. Rifling is not restricted to rifles alone. Pistols and revolvers may also have rifled barrels and almost all modern ones do. The only modern small-arms that don't have rifling these days are some shotgun models.
Note that rifling doesn't make a bullet travel in a straight line, but it gives it a known predictable drift, which is almost as good as travelling in a straight line. For instance, if you can reliably predict that a bullet shot out of your gun will drift between 2 to 3 cm to the right, at a distance of 100 meters, then you can adjust your sights accordingly to compensate.
Rifling is done by forcing a cutter into the barrel, to cut grooves to the desired angle, pitch and shape. The parts of the barrel that are untouched by the cutter are called the "lands". It is not necessary for a rifle barrel to contain only a single groove. In fact, 4, 6, 8, 12 grooves etc. are much more common. For instance, AK-47s usually have 4 right-hand turning grooves with 1 complete turn every 235 mm, M-16A2 has 6 right-hand grooves with 1 turn every 7 inches (178 mm) etc.
The amount of rifling that a barrel has, has to do with the weight and shape of the bullets that the gun is commonly expected to fire. The rule of thumb is that the longer and heavier that a bullet is, the more spin it needs to keep it stable in the air. To make it spin more, a faster rate of twist is required. For instance, the M-16 A1 model had a barrel that made 1 turn every 12 inches. This was sufficient for the bullet from the US M109 cartridge that it was originally designed for. However, NATO wanted a cartridge that had better penetration against new body armors, so they designed a SS109 cartridge that was the same external dimensions as the M109, but a slightly differently shaped, longer and heavier bullet with a steel tip. Unfortunately, the M-16 A1 barrel could not keep the bullet from the SS109 stable in flight beyond 90 meters, since it needed to spin this bullet faster (1 turn in 9 inches was required for SS109). The M16 also needed to fire the NATO L110 tracer bullet cartridge from the same rifle and this bullet needed an even faster rate of spin than the SS109. Hence, when they redesigned the rifle (the M-16A2) they changed the barrel's rate of twist along with adding some other features to the new rifle. This new rifle got a barrel that was 1 turn in 7 inches and could shoot the SS109 and L110, as well as the older M109 cartridge.
On the other hand, putting too much twist on the rifle barrel makes it wear out faster. Faster twist barrels also have more friction and drag and hence slow down the bullet a bit more. Also with high velocity bullets spinning too much, the bullets can literally tear themselves apart in flight, due to the centrifugal forces. Hence, the correct rate of rifling twist strikes a balance between these various factors.