It can either be by watching what lots of people do and eat over long periods of time (called epidemiology)
Also with experimental studies where some people are given one food and others given a control food ( called a controlled trial). These are then analysed and discussed to identify what is good and not
I guess initially we must have learnt what was good and bad for us through trial and error and then waiting to see the results/side effects. Food appearance, colour, texture, smells probably have a large influence too.
A combination of the answers form the guys here. But also anything that grows naturally is generally thought to be a good source of what we need for our body. So fruit and vegetables are really good as they grow naturally. Foods that are processed, so that require human manipulation to be formed aren;t as good for us, especially mass produced products in factories as they tend to add lots of chemicals to preserve the food, and add salts, sugars and fats to make it taste better if the ingerdients aren;t of good quality or fresh.