Whether powered by mains electricity, batteries or petrol, the principle behind a grass trimmer is the same.
A disc at the end of a long shaft, partially protected by a housing, is powered by the engine/motor and rotates at high speed.
Attached to the disk is a length of line, made of nylon or similar, which spins very fast and cuts the grass. As the line is nylon or a similar plastic, it will not damage trees, ornaments, walls and so on but will effectively cut through grass, chopping it up and clearing it quite effectively.
Power, features and some functions vary widely and there are some grass trimmers which have two lines rather than one for even more efficient cutting.
Also, not all line is the same. Larger machines use thicker line for larger and tougher tasks.