Cactuses are called "stem succulents" because they store water in the stems rather than in leaves or roots. When water is plentiful, the stems swell. During droughts, the plants use this water to stay alive and the stems gradually shrink. Some cactuses have ribbed stems that can expand like an accordion.
The roots of the desert cactus need a well-aerated soil that dries out quickly. If the roots stay moist, they will rot and this is the primary reason why a desert cactus will never colonize wet, coastal forests. Lack of sun is another reason why a forest is inhospitable to cactus. There are, however, some desert-type cactus that will grow on sandy coastal beach margins. Here they have the bright sun they need as well as quick drainage.
A soil that is high in sand or perlite is essential for success with desert cactus. This gives both quick drainage and an airy texture. Bright light is also important and can be supplied indoors by fluorescent lights or a sunny window. Water cactuses regularly while they are growing during spring and summer, but be sure the soil dries out a bit more than most houseplants between waterings. Let them get quite dry in winter, though, watering only once or twice a month.
Rain forest cactuses are epiphytes, living on the branches of trees far above the ground. Their stems are flattened with a thinner surface than desert cactus, but the family tolerance to drought allows them to live in pockets of organic matter. They are tolerant of partial shade and high humidity but still need the excellent drainage their epiphytic lifestyle provides. They'd never be found on a forest floor.
Give these cactuses the kinds of conditions they'd find in their native trees: bright light but no direct sun, medium to high humidity, regular watering while they are actively growing and a quick-draining potting mix with lots of organic matter. They can be kept rather root-bound in small pots for the size of the plant, and most kinds appreciate a resting period when they are kept cool and dry for several months.