In the wild guinea pigs live in groups of 10 or more, they are social creatures, and like company. As pets they are usually kept in pairs; two females (sows) will live happily together, as will two males (boars), particularly if they are brothers. Two boars of different ages will usually live together, as long as there are no females around. If a female and male live together the male should be castrated, otherwise you will end up with many more guinea pigs than you were expecting! When you buy two guinea pigs do check that they have been sexed properly so that you don't end up with any small surprises.
If you start with a pair, and are unlucky enough to suffer a bereavement you can usually reintroduce another guinea pig to your single one. If you do just keep a single guinea pig then they can derive company from a soft toy the same size as themselves, or you can give then a mirror, that way they see another guinea pig all day but get to keep all the food for themselves!
If you do buy a male and female, intentionally or otherwise, they will be capable of breeding from 3-4 wks of age. They will need to be kept separate until the male is old enough to be castrated, and the earliest this can be done is at 4 months of age.
What about guinea pigs and rabbits?
Although there are exceptions to every rule, and you may know of someone who keeps a rabbit and guinea pig together, this is generally not a good idea. A rabbit can easily hurt a guinea pig with its powerful back legs, even unintentionally, should it stamp its feet and kick the guinea pig by mistake. They also have different dietary requirements, guinea pigs need lots of vitamin C and rabbit food does not contain any. Finally the rabbit can carry a bacterial infection called Bordetella which does not affect the rabbit, but might make the guinea pig very sick if it catches it.