For the purpose of this question, I will focus on the First World War and the British Empire. In Great Britain, the 1907 Manual of Military Law allowed men of non-European ancestry to enlist in the British Army. This meant that during the First and Second World War, non-white men were not only accepted but integrated into otherwise all-white regiments. Simultaneously, however, Great Britain made sure that black soldiers from colonial regiments were forbidden from fighting on the Western front.
For the British Empire, there were two main arguments against the inclusion of racial minorities from British colonies in active combat roles. First, and perhaps most importantly, was the protection of the racial hierarchy which was at the very core of British imperialism. By allowing men of color from the colonies, in particularly of African descent, to fight against and even kill white men would upset the status quo which in turn might have larger consequences. The second argument revolves around racial prejudice and the idea that men of color simply did not have the intellectual or emotional capability to be good soldiers and were inferior to white soldiers. The result of this thinking meant that although colonial soldiers of African descent were allowed to serve in uniform, they were not allowed to fight on the Western front against white Germans and were instead given other roles working with different non-combat tasks. Outside of the Western front, however, the use of soldiers of color in combat roles were far more widespread. For pragmatic reasons and since the enemy was considered "less white" than western Europeans, soldiers from the British West Indies fought against Ottoman soldiers in the Middle East. The largest exception of them all were soldiers from British India that served on the Western front, the Middle East, and in Africa. The inclusion of racial minorities from British colonies as combat soldiers on the Western front during the First World War is therefore a long and complicated process which expressed itself in various and contradictory forms from one imperial army to the other.
In that sense, segregation was something that both existed and did not exist at the same time. If anything, it is a testament to the contradictory and illogical racism that was widespread in Great Britain at the time. Racial ideas, such as the 'martial races' concept so readily attached to soldiers from the British Indian Army (in particularly the Gurkhas), was used to both encourage and glorify the participation of non-white soldiers, while the racial notion of black inferiority guaranteed that soldiers of African descent serving in British colonial regiments were effectively barred from fighting against white men.
This institutional racism should be considered alongside the personal racism that non-white soldiers faced. It is difficult to give a general picture of what racism towards non-white soldiers looked like amongst soldiers themselves. One black British soldier, Norman Manley, wrote in his post-war memoir that as "far back as 1914 after the start of the first World War it was impossible to be in England and not be aware of the problem of colour. You were immediately aware in a thousand ways that you belonged elsewhere but not there." This was a shared experience by non-white British citizens at the time. Despite the fact that your family had lived in London, Edinburgh, or Manchester for generations, you were still singled out for your race. You became even more visible if you were wearing a uniform. The Liverpool Echo, for example, made sure to let its readership know in June 29, 1915, that the "stipendiary magistrate made it clear today that black men in Liverpool must not be insulted whilst they are wearing the King’s uniform." Black British men who tried to enlist were sometimes rejected by recruiters who saw their race as a disability, likely referring to the idea of black men being incapable of being soldiers.
Yet once in uniform and fighting, some black British soldiers found themselves gaining the trust of their white comrades and being defended against racist attitudes. Manley discovered that his fellow white soldiers could show "an innate courtesy, I suppose because we liked each other, and soon found out that I did not like being called “Darkie” as came natural to them, and I have heard a real tough guy get a hold of a new arrival, a casualty replacement, who automatically called me “Darkie”, and take him aside and say, “Don’t call him that – he doesn’t like it. We call him Bill and we like him!"
Due to the considerably few surviving sources written by black British soldiers, we are unable to confirm or deny whether Manley's experience was universal, but it does provide us an insight into how things looked like from the perspective of a black British soldier. Racial prejudice was present, but there were also ways of breaking through it. Some men might have not experienced any personal racism throughout their participation in the war, only to face racism upon returning home. The anti-black collective violence that took place throughout Great Britain in 1919 shows how complex this subject really is.
In conclusion, racism was very much present in the black British experience -- from whether or not they were even considered soldiers, to if they would be allowed to wear a British uniform, to the reactions from their white comrades.
Army of Empire: The Untold Story of the Indian Army in World War I by George Morton-Jack does a thorough job addressing your question (only WW1 of course.) It lines up with Bernardito's very good response, but here are a few extra details that I recall -