How the banding in agate forms is a bit of a mystery. Here's a high-level of what we know so far.
Most agates form inside cavities in basalt, a volcanic rock. When lava erupts from the ground it has bubbles of fluid, which can be liquid or gas. As the rock cools the fluids are released, but this creates a bunch of holes or vesicles in the basalt. Later, silica-rich water percolates through the basalt, and the agate crystallizes out of the water. Usually, about 90% of this silica crystallizes as quartz and 10% is an unusual form of silica called moganite. The outer layer in an agate is usually chalcedony, a mixture of quartz and moganite. But, how agate's signature banding occurs is a mystery.
There are a couple theories, though. Perhaps the silica and water forms a silica jello where crystallization is occurring, and trace elements in the jello produce the color in the bands. Another theory, from Penn State Professor Peter Heaney, suggests the silica-rich water could with enough silica become polymerized, which could make crystallization occur quickly. The banding would result from the waxing and waning availability of silica.
But, nobody knows because nobody has synthesized agate. So, maybe put that on your to do list.
Agate doesn't just fill holes in basalt. We have examples of it filling holes left after volcanic ash incinerated a tree and of it replacing the structure a coral fossil. See our "agate" search results for more.