My textbook has this sentence: "Ich kaufe Obst und Gemüse ein" but I don't understand what's the need for "ein" at the end. Is "Ich kaufe Obst und Gemüse" fine or is it necessary to use the word "ein" at the end?
The two sentences just use different verbs, "einkaufen" vs. "kaufen". Both verbs basically mean "to exchange goods against money", but the nuances are slightly different.
"Kaufen" is kind of a everyday, general verb for the meaning. "Einkaufen" puts the focus a little bit more on aquiring the goods. So, if you'd say "Ich kaufe Obst und Gemüse ein", there's a small implication that you needed those goods and now have them. Also, "einkaufen" is more used for purchasing multiple things (for example, grocery shopping on the weekend), and "kaufen" is more used for purchasing one single, possibly expensive, thing. But the differences are really small.
Additionally, "kaufen" is typically used as a transitive verb, so it needs an object. "Einkaufen" can be used with or without an object:
Ich kaufe das Obst.
Ich kaufe.(would need an object)
Ich kaufe das Obst ein.
Ich kaufe ein. (fine without an object)
You might compare the two verbs with the English pair of "to buy" and "to purchase". The difference is really small there, too (though it's a different difference ;) ).
In this case it's fine to use it without "ein".
But "kaufen" and "einkaufen" aren't the same thing in general. Both can be used if you pay someone for getting some product. "kaufen" is only used for this (and for "bribe" in slang). "einkaufen" often includes going to the store where you actually will buy something and it's also used like "buy in" (in casino games, a firm, etc.).
A difference in usage is that "kaufen" needs a description of what you buy: "Ich kaufe Gemüse." or "Ich gehe Gemüse kaufen." while "einkaufen" doesn't: "Ich kaufe ein" or "Ich gehe einkaufen." but it would be ok to use it with a description: "Ich kaufe Gemüse ein." or "Ich gehe Gemüse einkaufen.". In this case the words are used like "buying" and "shopping".
Others have already focused on grammar, so I want to focus on practice.
In daily usage, einkaufen is usually the verb if you're getting groceries or other items of daily use.
"Ich muss später noch einkaufen".
If you say this, most people will assume you'll go to the supermarket. As other users have stated, this doesn't need on object.
Kaufen can only be used with an object, so you use it to highlight specific items you want to get, i.e.
"Kannst du noch Bratwürste kaufen?"
If you just want to stroll, you can use the germanised verb "shoppen"
"Wir waren heute in der Mall shoppen"
A German equivalent would be "bummeln".