Question: Was Guangdong a part of Vietnam in the past? How did Vietnam lose Guangdong in China?
Answer: I think that it depends on the term of Vietnam you understand, my answer is Yes and also No
Firstly , The answer is No if Vietnam refers to land or geography as the modern sovereignty state today. The Guangdong region is never the land of Vietnam.
Secondly, If you think that the concept of Vietnam is the land of all of the Viet ethnics and Viet kingdoms, My answer is yes - Guangdong is a former land of the Vietnamese ancestors.
Ancient time: Before the conquest of the Qin Empire (221–206BC) (Ancient China) into the lands in Southern China today including the Guangdong region around 218 BC. The Han people/ Qin people (Chinese) were never native of the Guangdong region but Vietic people/Bach Viet tribe were really native and this region really had belonged to Vietic people.
(Chinese land under the Zhou Dynasty before the Qin Empire/Dynasty 221–206BC and Southern China today are the land of Vietic people/Baiyue/Bach Viet).
(China under Qin Empire/Dynasty and started to invade the land of the Vietic people in the Southern China today )
According to historical evidence , the historical researchers think that it can be roughly defined: the Qin army started the invasion of Bach Viet in 218 BC.
According to Huainan Zi, 500 thousand Qin soldiers were divided into five armies, in which two armies garrisoned in Dam Thanh and Cuu Nghi Mountain Passes invaded Quang Tay, the land of Tay Au (or Au Viet). Their food boats went upstream Tuong River which flowed from Ngu Linh Mountain to Dong Dinh Lake. However, when they reached the origin of Tuong River, there was no waterway to Ly River aka Que River, to enter Quang Tay. So, Do Thu commanded Giam Loc to “order troops to dig canals to transport food” (Huainan Zi). That was Linh Cu Canal or Hung An Canal connecting the Tuong River to Ly River, which still exists. Loc was a Viet resident who was holding the position of court counselor under the Qin Dynasty. Because Loc understood the terrain of Linh Nam and was good at waterway, Do Thu assigned him to take charge of the transportation of food and digging of Linh Cu Canal to connect the Tuong River to Ly River.
In the 3 years (218 – 215 BC) since the dispatch of troops, the Qin had to dig a canal and deal with the struggle of the Viet people, thus “three-years without taking off armors” (Huainan Zi). Then, thanks to Linh Cu Canal, the Qin soldiers moved to the Ly River (Que River) and then into the Tay Giang River basin which was the area of the Tay Au tribe. Qin troops killed a chief of the Tay Au tribe, i.e. Dich Hu Tong, but they had to face fierce resistance of the Viet people.
In 214 BC, the Qin conquered Luc Thuong and set up the land into three districts: Nam Hai, Que Lam, and Tuong. Nam Hai was the Guangdong region (invaded by the third army). Que Lam was the northern and eastern part of Quang Tay. Tuong County was in the west of Quang Tay and a part of southern Que Chau. Thus, the three districts of Nam Hai, Que Lam, and Tuong captured by Qin troops were within Quang Dong, Quang Tay, and a part of Que Chau in southern China. When setting up Nam Hai, Que Lam, and Tuong Districts, Qin troops had entered the Tay Giang basin and basically occupied this area. On the verge of victory, with the favorable waterway for transporting food, of course, Qin soldiers did not stop there. From Tay Giang, Qin troops could go downstream Ta Giang and Ky Cung Rivers to the north and northeast of Vietnam. Tay Au and Lac Viet tribes stood up to fight against the Qin invaders. There was no record of this struggle; however, Ly Ong Trong legend partly reflects the clash between the Qin and An Duong Vuong.
Linh Nam kingdom (Vietnam) 40–43 AD led by the Trung sister (Vietnamese emperor)
According to Vietnamese history, Nanhai as the Guangdong province today had belonged to Linh Nam kingdom (Ancient Vietnam)
In conclusion : With logical thinking about the land inherited from the Vietic people and also the Linh Nam kingdom, Guangdong today is really a former land of the Vietnamese. And, Vietnam lost to China around 2000 years because of being invaded by the Chinese people under the Qin Dynasty and the Han Dynasty.
P/s: I write answers based upon the gist and requirement of the question. Readers are requested to read it with neutrality and rational approach. I throughout this answer has no intention of showing any disrespect towards any group of people or country.