Guinea is a country in Africa where sailors often went before returning home to Europe. It is unknown if the word guinea referred to the country of Guinea in Africa that Europeans thought their new pets were coming from, if it is a misspelling of French Guiana in South America, closer to where the cavies are from, a reference to a far-away place (as the word guinea referred to back then), or if it is in reference to the sailors who brought the pigs to Europe (unexperienced sailors were often referred to as guinea pigs).
The guinea pig has also been known as a cavy, which refers to the Latin name for guinea pig, Cavia porcellus, and means "little pig."
The words "ginny" and "guinea" have more recently been used as a derogatory slang word for Italian-Americans, similar to the "N-word" for African-Americans.
The phrase "guinea pig" has been used since the early 1900's to describe someone that is testing something out for the first time, such as in a lab experiment. Guinea pigs were and still are used for research experiments so the common phrase came about naturally.