That’s a very good question! I’m not sure if we completely know how they do it, but we have some theories. It is possible that it’s a purely instinctive thing for them to do. On the other hand, a lot of their knowledge might come down to experience. When they first hatch, they’ll be brought up in a nest, so they’ll have an idea of what one should look like. When it eventually comes to building one for themselves, they might use this mental image as a sort of plan, and then use trial and error to find out what sorts of building materials (like sticks and grass) work better than others.
However, this raises the question of whether birds actually think in this way, and that’s something that we don’t know too much about at the moment.