Reducing the distance food has to travel has two advantages: reducing how much CO2 and other pollutants are emitted, and choosing vegetables that taste good and are harvested ripe instead of cultivars that are only good at travelling (unripe).
Farms located in buildings are often called "vertical farms" because they can be over several floors (potentially entire skyscrapers) and there can be several levels of plants upon one another on each floor. Obviously these systems are limited to vegetables and some fruits — there is no question of wheat (let alone paddy fields) in a building.
Farms are normally found in the countryside, where land is rather cheap. Vertical farms are in urban areas, where land is expensive. Moreover, even if it were economical, food production may then compete with housing. And while it is possible for vegetables to grow in buildings, it is not possible for people to live in fields. A building full of strawberries instead of inhabitants would do little good to those with poor access to good food if it made them homeless. This is very much like the problem of biofuels displacing food, and it has the same sustainability issues.
Canada's Lufa farms solved these problems by setting up their greenhouses on the roofs of factories, thereby
Another solution is stacking up plants on top of one another. With ten levels, 100 m2 worth of vegetables can be grown in a room of 10 m2. A lot less floor space is needed, but it is impossible to rely on sunlight. Each rack must thus have its own light source (e.g. LED). And it is quite silly to boast a lower environmental impact because the food does not have to be transported over 100 km if the photosynthesis for the entire farm runs on coal-based electricity. Agriculture is based on turning (free, renewable, zero-CO2) sunlight into food; vertical farms with artificial lighting turn coal or natural gas into food — how could they be part of our sustainable future?
One may counter that the electricity could come from solar panels. But due to their efficiency of 15–20% it takes 5–6 ha of solar panels to shine light on 1 ha of culture (assuming 100% efficiency for the light bulbs). And if there are ten levels of racks then 200 m2 of floor space require about 1 ha of land for its electricity. Such farms do take up a lot of space, but it happens out of sight. Just like factory-farms raising cattle on corn and soybeans rather than grass, the large land need is hidden in the inputs.
In the case of northern cities like Montreal, Boston or New York (where some of the rooftop greenhouses are set up), heating is necessary. This obviously increases both the cost and the environmental impact of the food. One should then wonder whether it is better to grow tomatoes further south in unheated greenhouses and transport them north. Put otherwise, is transportation really worse than heating? (I do not have the answer.)
Note that it is not just about the distance between producer and seller: if you buy lettuce that was grown down the street from where you live but was washed and packaged 1 000 km away, the total distance travelled by the food is 1 000 km (and the pollution is to scale).
Growing vegetables in a building is bound to be somewhat different from doing so in fields. First, it is hard to drive your tractor through the door. Then there is the question of soil. It is quite possible to grow plants in pots. Another possibility is to do without soil altogether. This is hydroponics.
Soil plays two roles: the roots of the plant can hang to it to literally hold its ground, and the soil also conveys water and nutrients to the plant. But inert materials such as gravel and coconut fibers can do the former. And water containing nutrients can be flown (or sprayed in aeroponics) directly onto the roots for absorption. In a word, it is possible to grow plants without soil. And since soil is heavy, hydroponic rooftop greenhouses have the advantage of being compatible with the maximum load the roof can bear.
Aquaponics goes a step further: it relies on symbiosis between fish and plants. The waste from fish is used as manure to feed the plants, and the plants clean the water. Fish and vegetables are both sold. This is the 21th century version of the old mixed farm with crops and animals (chicken and pigs fed on dropped grain, and leftovers and their wastes fed the crops).
Conventional agriculture feeds its crops with synthetic fertilizers, whereas nature (and some modern agricultural systems) feeds the soil that in turn feeds the plants. To conventional agriculture, soil is an inert substrate on which roots can hold and which can be soaked with water and nutrients; organic matter and life are superfluous. Hydroponics does the same but with more honesty, using substrates like perlite instead of vampirized soil. (This is a discussion soil nerds find cool; label nerds on the other hand will be disappointed to learn that hydroponic farms generally do not fit the criteria for organic labels).
While, in soil terms, hydroponics is further away from organic agriculture than even conventional agriculture is, it can be very much like it in other respects:
A vegetable garden in a corner of one's backyard requires very little to start: a bit of space, a few seeds (soil is provided by nature and compost can be produced for free). A hydroponic greenhouse demands a lot more capital. Which limits who can build one (well-financed companies in rich countries). And which increases the risk: a big hailstorm or hurricane will not just destroy the crop but also the entire infrastructure, possibly leading to bankruptcy. To put to good use an industrial wasteland, where weight is not a constraint, the low-tech soil-based solution is easier to finance and lower risk.