Semiconductor nanowires have received much attention owing to their potential use as building blocks of miniaturized electrical, nanofluidic and optical devices. Although chemical nanowire synthesis procedures have matured and now yield nanowires with specific compositions and growth directions, the use of these materials in scientific, biomedical and microelectronic applications is greatly restricted owing to a lack of methods to assemble nanowires into complex heterostructures with high spatial and angular precision. Here we show that an infrared single-beam optical trap can be used to individually trap, transfer and assemble high-aspect-ratio semiconductor nanowires into arbitrary structures in a fluid environment. Nanowires with diameters as small as 20 nm and aspect ratios of more than 100 can be trapped and transported in three dimensions, enabling the construction of nanowire architectures that may function as active photonic devices. Moreover, nanowire structures can now be assembled in physiological environments, offering new forms of chemical, mechanical and optical stimulation of living cells.