All sorts of things are used to inform decisions, but it's far from random. Classes need to be as evenly split as possible in terms of SEN, gender, pupil premium, summer borns etc. A rough list may be drawn up based on these known factors. Then schools get chatting to nurseries and the list may be tweaked based on personality factors.
Nurseries often advise who to keep together and who to keep apart, in cases where they have multiple children transferring to the same school. It's not always possible to accommodate every piece of advice, some friendships may naturally have to be split to get the balance of other factors right (on the basis of nursery advising that actually, although those 2 are good friends they're also both good at making new friends). The most likely personality factors to be accommodated are the personality clashes - who are definitely best kept apart? (either due to constant arguing, or silliness together).
Obviously it's impossible to get the perfect mix, as there will always be some unknowns on the list in terms of personality or how they'll get on with the others (due to them being the only one transferring from their setting, or not attending a previous setting at all) but even then their gender and DOB etc will be taken into account in terms of where is best to place them.
A lot of thought and time goes into producing class lists, which is why it can be so frustrating when parents try to get involved requesting that their child absolutely must be kept with their best friend. Sometimes requests are possible (or have already been factored in on nursery advice, if it's deemed best for those children) but often it's a logistical nightmare to tweak the lists to accommodate parental requests and, sometimes, what the parents want isn't ultimately what is best for those individual children. Some children focus best on a carpet space away from their best friend, especially in a setting where the two classes mix a lot and therefore they'll still see a lot of each other anyway.
Obviously in one form entry schools you just get what you're given. Some classes are heavily unbalanced on gender, ability, needs etc and other years it's a much more even mix. Some years you get children who you really wish you could have separated if they were at a larger school. Other years the kids all get on like a house on fire. However, in schools with one or more classes in each year group it's best to try and split them as evenly as possible, as it's not fair on the children if one lot end up in a class which is generally calm and the others end up in an environment which is far more stressful for whatever reason. We try to give them as equal and opportunity and experience as possible.